博客
关于我
C语言/C++ 文件操作
阅读量:257 次
发布时间:2019-03-01

本文共 1793 字,大约阅读时间需要 5 分钟。

C???C++??????

C??????

1. ????

?C??????????????fopen()???????????????????FILE *fp????????????????

2. ????

???????\???????????\?????\\????????????????

  • ????"r"??????????
  • ????"w"???????????????????
  • ????"a"???????????????????????
  • ????"r+"??????????
  • ??????"w+"??????????????
  • ??????"a+"???????????????

??????????????"b"???????"rb"??????????"wb"??????????

3. ????

??fclose()?????????????????????????????????????????

4. ????

  • ???????fgetc()????????????feof()??????
  • ???????fputc()????????????
FILE *fp = fopen("file.txt", "r");
char ch;
while (!feof(fp)) {
ch = fgetc(fp);
// ????
}
fclose(fp);
  • ???????fputs()?fprintf()???????????

5. ?????

  • ????????fgets()?????????????
  • ????????fputs()?fprintf()???

6. ?????

??fscanf()?fprintf()?????????????

int rollnumber, score;
char name[20];
FILE *fp = fopen("data.txt", "r");
while (!feof(fp)) {
fscanf(fp, "%d %s %d", &rollnumber, name, &score);
}
fclose(fp);

7. ????????

  • ??????rewind()????????????
  • ??????feof()????????
  • ??????fseek()??????????????????????
  • ???????fread()?fwrite()??????????

C++????

C++?????????????????ifstream???????ofstream???????fstream?????????

1. ???????

  • ifstream??????
  • ofstream??????
  • fstream??????

2. ????

??open()????????????

ofstream file;
file.open("file.txt", ios::out | ios::app | ios::binary);
// ?
fstream file("file.txt", ios::out);

3. ????

??close()???????

ofstream file;
file.open("file.txt");
// ...??
file.close();

4. ????

  • ???????<<?>>????????
  • ????????put()?get()?read()?write()???
ofstream file("file.bin", ios::out | ios::app);
file.put('A');
file.close();
ifstream file("file.bin", ios::binary);
char ch;
file.get(ch);

5. ????

  • eof()`??????????
  • seekg()?seekp()????????
  • read()?write()???????
fstream file("file.txt");
file.seekg(5, ios::beg); // ????5???
string str(10);
file >> str;

??????????????????????????????????

转载地址:http://vllx.baihongyu.com/

你可能感兴趣的文章
npm错误 gyp错误 vs版本不对 msvs_version不兼容
查看>>
npm错误Error: Cannot find module ‘postcss-loader‘
查看>>
npm,yarn,cnpm 的区别
查看>>
NPOI
查看>>
NPOI之Excel——合并单元格、设置样式、输入公式
查看>>
NPOI初级教程
查看>>
NPOI利用多任务模式分批写入多个Excel
查看>>
NPOI在Excel中插入图片
查看>>
NPOI将某个程序段耗时插入Excel
查看>>
NPOI格式设置
查看>>
NPOI设置单元格格式
查看>>
Npp删除选中行的Macro录制方式
查看>>
NR,NF,FNR
查看>>
nrf24l01+arduino
查看>>
nrf开发笔记一开发软件
查看>>
nrm —— 快速切换 NPM 源 (附带测速功能)
查看>>
nrm报错 [ERR_INVALID_ARG_TYPE]
查看>>
NS3 IP首部校验和
查看>>
NSDateFormatter的替代方法
查看>>
NSError 的使用方法
查看>>