文件读写FILE类

1. 新建一个文件:

  FILE *f = fopen("a.txt","w+");

(1)fopen()函数介绍
fopen的原型是:FILE *fopen(const char *filename,const char *mode),fopen实现三个功能:
  为使用而打开一个流 
  把一个文件和此流相连接 
  给此流返回一个FILR指针
参数filename指向要打开的文件名,mode表示打开状态的字符串,其取值如下
字符串 含义 
"r" 以只读方式打开文件 
"w" 以只写方式打开文件 
"a" 以追加方式打开文件 
"r+" 以读/写方式打开文件,如无文件出错 
"w+" 以读/写方式打开文件,如无文件生成新文件 

2.写文件

CString str;
str.Format(L"1\t%d,%d\t",170,30);
fwrite(str, 1, sizeof(str)*str.GetLength()/2, f);

 

3.关闭文件

fclose(f);

 

4.fscanf和fprintf读写文件

fscanf和fprintf可以按行读写,

int d;

fscanf(f,"%d",&d);

int c = 10;

fprintf(f,"%d\r\n",c);

 

posted @ 2017-12-03 13:08  单纯的心  阅读(1320)  评论(0)    收藏  举报