欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

有关fwrite语句的用法

发布时间:2023/12/2 50 豆豆
生活随笔 收集整理的这篇文章主要介绍了 有关fwrite语句的用法 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

一:函数名: fwrite

  功 写内容到流中

  用 :fwrite(buffer,size,count,fp);

  (1buffer:是一个指针,对fwrite来说,是要输出数据的地址。

  (2size:要写入的字节数;

  (3count:要进行写入size字节的数据项的个数;

  (4fp:目标文件指针。

  程序例:

  • #include <stdio.h>
  •   struct mystruct
  •   {
  •   int i;
  •   char ch;
  •   };
  •   int main(void)
  •   {
  •   FILE *stream;
  •   struct mystruct s;
  •   if ((stream = fopen("TEST.$$$", "wb")) == NULL) /* open file TEST.$$$ */
  •   {
  •   fprintf(stderr, "Cannot open output file.\n");
  •   return 1;
  •   }
  •   s.= 0;
  •   s.ch = 'A';
  •   fwrite(&s, sizeof(s), 1, stream); /* write struct s to file */
  •   fclose(stream); /* close file */
  •   return 0;
  •   }
  • fprintf的区别

    fprintf(fp, "%d", buffer); 是将格式化的数据写入文件
    fprintf(文件指针,格式字符串,输出表列); 

    fwrite(&buffer, sizeof(int), 1, fp);是以二进位位方式写入文件
    fwrite(数据,数据类型大小(字节数),写入数据的最大数量,文件指针); 

    由于fprintf写入时,对于整数来说,一位占一个字节,比如1,占1个字节;10,占2个字节;100,占3个字节,10000,占5个字节
    所以文件的大小会随数据的大小而改变,对大数据空间占用很大。
    fwrite是按二进制写入,所以写入数据所占空间是根据数据类型来确定,比如int的大小为4个字节(一般32位下),那么整数10所占空间为4个字节,10010000所占空间也是4个字节。所以二进制写入比格式化写入更省空间。

    因此,
    对于1 2 3 4 5 6 7 8 9 0 十个整数,用fprintf写入时,占10个字节;而用fwrite写入时,占40个字节。
    对于100 101 102 103 104 105 106 107 108 109 110 这十个整数,用fprintf写入时,占30个字节;而用fwrite写入时,占40个字节。
    对于10000 10100 10200 10300 10400 10500 10600 10700 10800 10900 11000 这十个整数,用fprintf写入时,占50个字节;而用fwrite写入时,还是占40个字节。



    二:实例 可借鉴试读过程  转自:http://topic.csdn.net/t/20010530/11/139946.html

  • #include <stdio.h> 
  • #include <string.h> 
  • #include <conio.h> 
  • FILE *stream;//, *stream2; 
  • FILE *stream2; 
  • void main(void) 
  • { 
  •   int numclosed; 
  •    char *list; 
  •    list= "这个程序由阳永红编写 "; 
  •     if((stream= fopen("data.txt ","r "))==NULL) //试图打开文件data.txt,如果该文件不存在,则自动创建 
  •        { 
  •           printf("试图打开'data.txt '\n"); 
  •           printf(" 'data.txt '不存在\n "); 
  •           printf(" 'data.txt '被创建\n "); 
  •         } 
  • else 
  •      printf(" 'data.txt '被打开\n "); //以写入方式打开
  •     if( (stream2=fopen("data.txt ","w+ "))==NULL) 
  •      printf( " 'data.txt '不存在\n " ); 
  •     else 
  •         { 
  •             printf( " 'data.txt '成功被打开\n " ); 
  •           fwrite(list,strlen(list),30,stream2); 
  •           printf( "写入数据成功\n "); 
  •         } 
  • if(stream!=NULL) //如果data.txt存在就会打开成功,则stream!=NULL,这时就关闭stream 
  • if(fclose(stream)) 
  • printf("文件流stream被关闭\n " ); 
  • numclosed=_fcloseall( ); //关闭所有打开的文件流,返回关闭的文件流个数
  • printf("被关闭的文件流量: %u\n ", numclosed ); 
  • _getch(); //按任意键后退出 
  • }
  • =============================================================================


    三:数据块读写函数fread和fwite

  • #include <stdio.h>
  • #include <math.h>
  • #include <stdlib.h>
  • #include <conio.h>
  • #define N 5
  • struct student
  • {
  • char name[20];
  • int num;
  • int age;
  • char addr[15];
  • };
  • int main()
  • {
  • /*数据块读写函数fread和fwite
  • 读取数据块函数调用的一般形式为
  • fread(buffer,size,count,fp)
  • 写数据块函数调用的一般形式为
  • fwrite(buffer,size,count,fp)
  • 其中:
  • buffer是一个指针,在fread函数中,它表示存入数据的首地址;
  • 在fwrite函数中,它表示输出数据的首地址
  • size表示数据块的字节数
  • count表示要读写的数据块的块数
  • fp表示文件指针
  • fread(array,4,10,fp)
  • 表示从fp所指的文件中,每次读4个字节(一个实数)送入实数数组array中,
  • 连续读取10次,即读10个实数到数组array中
  • 从键盘输入N个学生的数据,写入一个文件中,再读出这N个学生的数据显示到屏幕上
  • */
  • struct student stua[N],stub[N],*pp=stua,*qq=stub;
  • FILE *fp;
  •     int i;
  • if((fp=fopen("c:\\happy.txt","w+"))==NULL)
  • { 
  • printf("Cannot open file,press any key exit!\n");
  • getch();
  • exit(1);
  • }
  • printf("Iput data \n");
  • for(i=0;i<N;i++,pp++)
  • {
  • printf("请输入第%d位学生的数据:",i+1);
  • scanf("%s%d%d%s",pp->name,&pp->num,&pp->age,pp->addr);
  • }
  • pp=stua;
  • fwrite(pp,sizeof(struct student),N,fp);//写N个学生的记录
  • rewind(fp);
  • fread(qq,sizeof(struct student),N,fp);//读N个学生的记录
  • printf("\n\n name number age addr \n");
  • for(i=0;i<N;i++,qq++)
  • printf("%-10s%-6d%-5d%-15s\n",qq->name,qq->num,qq->age,qq->addr);
  • fclose(fp);
  • return 0;
  • }
  • 总结

    以上是生活随笔为你收集整理的有关fwrite语句的用法的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。