首页 > 编程语言 > 详细

C语言中文件读写参考

时间:2014-01-23 00:38:51      阅读:493      评论:0      收藏:0      [点我收藏+]

C语言中的文件读写分为两种,一种是二进制文件读写,一种是文本文件读写 这里的区分主要是在打开文件时的第二个参数的选择 

fopen的第二个参数常见的形式有

“rt”      只读打开一个文本文件,只允许读数据 
“wt”      只写打开或建立一个文本文件,只允许写数据
at”      追加打开一个文本文件,并在文件末尾写数据
“rb”      只读打开一个二进制文件,只允许读数据
“wb”       只写打开或建立一个二进制文件,只允许写数据
“ab”       追加打开一个二进制文件,并在文件末尾写数据
“rt+”      读写打开一个文本文件,允许读和写
“wt+”      读写打开或建立一个文本文件,允许读写
“at+”      读写打开一个文本文件,允许读,或在文件末追加数据“
rb+”      读写打开一个二进制文件,允许读和写 
“wb+”      读写打开或建立一个二进制文件,允许读和写
“ab+”      读写打开一个二进制文件,允许读,或在文件末追加数据

 

 

1.通过例子来介绍fwrite和fread的用法

 

 

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3.   
  4. #define SIZE 2  
  5.   
  6. typedef struct student{  
  7.     char name[10];  
  8.     int num;  
  9. };  重生之大文豪www.dwhao.com
  10.   
  11. student stu[SIZE+1];  
  12.   
  13.    
  14. void save()  
  15. {  
  16.     FILE *fp;//http://blog.csdn.net/iaccepted  
  17.   
  18.     int i;  
  19.   
  20.     if((fp=fopen("t.txt","w"))==NULL){  
  21.         printf("Error!\n");  
  22.         exit(1);  
  23.     }  
  24.     for(i=0;i<2;++i){  
  25.         if(fwrite(&stu[i], sizeof(student), 1, fp) != 1){  
  26.             printf("ERROR!\n");  
  27.             exit(1);  
  28.         }  
  29.     }  
  30.     fclose(fp);  
  31.       
  32. }  
  33.   
  34. void get(){  
  35.   
  36.     FILE * f;  
  37.     if((f=fopen("t.txt","r"))==NULL){  
  38.         printf("Error!\n");  
  39.         exit(1);  
  40.     }  
  41.     //the following two sentences aim to test the function of fseek  
  42.     //fread(&stu[2],sizeof(student),1,f);  
  43.     //fseek(f,0,SEEK_END);  
  44.     fread(&stu[2],sizeof(student),1,f);  
  45.     fclose(f);  
  46. }  
  47.    
  48. void main()  
  49. {  
  50.     int i;  
  51.     for(i=0;i<SIZE;++i){  
  52.         scanf("%s%d",stu[i].name,&stu[i].num);  
  53.     }  
  54.     save();  
  55.     get();  
  56.   
  57.     printf("%s %d\n",stu[2].name,stu[2].num);  
  58. }  


2.通过例子来介绍fprintf 和 fscanf的用法

 

 

 

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4.   
  5.   
  6. typedef struct student  
  7. {  
  8.     char name[20];    //姓名  
  9.     int num;        //学号  
  10. }student;  
  11.   
  12. void main()  
  13. {  
  14.     FILE *pWrite,*pRead;//http://blog.csdn.net/iaccepted  
  15.     struct student tStu,tStu2;  
  16.     char *pName = "letuknowit";  
  17.   
  18.     if((pWrite=fopen("t.txt","w"))==NULL){  
  19.         printf("Error!\n");  
  20.         exit(1);  
  21.     }  
  22.       
  23.   
  24.     memcpy(tStu.name,pName,20);  
  25.     tStu.num = 1;  茶叶www.aichar.com
  26.       
  27.     fprintf(pWrite,"%d %s\n",tStu.num,tStu.name);  
  28.     fprintf(pWrite,"%d %s\n",tStu.num,tStu.name);  
  29.     fclose(pWrite);  
  30.   
  31.     if((pRead=fopen("t.txt","r"))==NULL){  
  32.         printf("Error!\n");  
  33.         exit(1);  
  34.     }  
  35.   
  36.     fscanf(pRead,"%d %s\n",&tStu2.num,tStu2.name);  
  37.     fclose(pRead);  
  38.   
  39.     printf("%d %s\n",tStu2.num,tStu2.name);  
  40. }  



 

用来记录一下常用的程序操作,仅供自己参考

C语言中文件读写参考

原文:http://www.cnblogs.com/jiangyea/p/3530146.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!