Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12297076
  • 博文数量: 1293
  • 博客积分: 13501
  • 博客等级: 上将
  • 技术积分: 17974
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 18:11
文章分类

全部博文(1293)

文章存档

2019年(1)

2018年(1)

2016年(118)

2015年(257)

2014年(128)

2013年(222)

2012年(229)

2011年(337)

分类:

2012-10-31 20:59:51

一、案例代码


  1. /****************************************************************
  2. * Name   : rondom_create_score.c
  3. * Author : dyli2000
  4. * Date   : 20121031
  5. * Description :
  6. * This file show how to create a rondom data and
  7. * write it to a txt.And show the function application of
  8. * file operate.
  9. ****************************************************************/

  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <time.h>

  13. #define TIMES 20
  14. //#define DEBUG

  15. int main()
  16. {
  17.     FILE *fp;
  18.     int count = 0;
  19.     char string[200];
  20.     int k = 0;

  21.     memset(string,'0',200);
  22.     fp = fopen("test.txt","w+");
  23.     if(fp == NULL)
  24.         perror("fail!");

  25.     /* step1,call srand.It's return value is "void" */
  26.     srand((unsigned int)time(0));

  27.     for(;count < TIMES; count++)
  28.     {
  29.         /* step2,call rand */
  30.         k = rand()%10;
  31.         #ifdef DEBUG
  32.         printf("----------------------------------\n");
  33.         #endif
  34.         sprintf(string,"Name %d ",k);
  35.         //fwrite(string,sizeof(char),strlen(string)+1,fp);
  36.         fwrite(string,sizeof(char),strlen(string),fp);
  37.         #ifdef DEBUG
  38.         printf("==================================\n");
  39.         #endif
  40.     }

  41.     /* seek to the endding of the file*/
  42.     fseek(fp,0L,SEEK_END);
  43.     /* count the file size */
  44.     int len_read = ftell(fp);
  45.     printf("len_read=%d\n",len_read);

  46.     /* seek to the beginning of the file */
  47.     rewind(fp); // <=> fseek(fp,0,SEEK_SET);

  48.     char read_buf[300];
  49.     memset(read_buf,0,300);
  50.     fread(read_buf,1,len_read,fp);
  51.     read_buf[len_read] = '\0'; // Very important
  52.     printf("%s\n",read_buf);
  53.     fclose(fp);

  54.     return 0;
  55. }


二、操作及运行效果


[root@localhost printTotxt]# vim test.txt

正常写入语句 fwrite(string,sizeof(char),strlen(string),fp);txt文件的显示效果。

image_thumb1


如果改为

fwrite(string,sizeof(char),strlen(string)+1,fp);

就会出现下面的图示,说明字符串的’\0’被写进了文本文件

image_thumb4


如果直接先操作

fread(read_buf,1,len_read,fp);

然后

printf("%s\n",read_buf);   

则只能输出第一个Name 3 ,因为读到第一个'\0'就断开了。

image_thumb6

 

故对于读写文件,上面代码的做法为合理的写法。主要步骤为:

(1)、fwrite(string,sizeof(char),strlen(string),fp); 写数据时不用写‘\0’到文本;

(2)、读的时候,读完后再加一个’\0’。如果不加,这样输出时才有可能出现乱码。

    fread(read_buf,1,len_read,fp);
    read_buf[len_read] = '\0';    // Very important
    printf("%s\n",read_buf);   

 

三、相关接口函数说明


1、linux调用系统函数产生随机数,需要将srand(),rand()两个函数合并使用。


srand(),返回值为void;rand(),返回值为随机数。

单独使用Rand(),每次返回的值都一样。必须先用srand(),用它的参数对随机数产生器进行初始化。常用技巧为使用时间做为随机数产生器的种seed,即srand(unsigned int)time(0))。

 

2、fopen,fwrite,fread,fseek,fclose等文件操作函数简介


 (1)、fp = fopen("test.txt","w+");  

//注意打开的方式 为w+而不是w,请分析原因。


r 以方式打开文件,该文件必须存在。

r+ 以可读写方式打开文件,该文件必须存在。

rb+ 读写打开一个,允许读数据。

rw+ 读写打开一个文本文件,允许读和写。

 

w 打开只写文件,若文件存在则文件长度清为0,即该文件内容会消失。若文件不存在则建立该文件。(注意,这个时候调用fread,是无法读出数据的)

w+ 打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件。(注意,这个时候调用fread,是可以读出数据)

a 以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。(EOF符保留)

a+ 以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。 (原来的EOF符不保留)

wb 只写打开或新建一个二进制文件;只允许写数据。

wb+ 读写打开或建立一个二进制文件,允许读和写。

ab+ 读写打开一个二进制文件,允许读或在文件末追加数据。


(2)、fwrite(buf,sizeof(char),strlen(string),fp),将buf中的数据写到fp中

代码段:

sprintf(buf,"Name %d ",k);

fwrite(buf,sizeof(char),strlen(buf),fp)

buf             : 数据读出的缓冲区;

sizeof(char) :每个元素的大小;

count          : 要读多少个元素;


(3)、fread() 读数据;

fread(read_buf,1,len_read,fp);

将fp中的数据读出到read_buf中。

read_buf : 数据读入的缓冲区
1      : 实则 sizeof(char),读入的每个元素的大小;
len_read : 读入的数据的长度
          

(4)、rewind 和 fseek(fp,0L,SEEK_END)

  /* seek to the beginning of the file */ rewind(fp); // <=> fseek(fp,0,SEEK_SET);


(5)、len_read = ftell(fp),获取文本的大小。



阅读(3115) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~