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

本文共 1767 字,大约阅读时间需要 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/

你可能感兴趣的文章
Objective-C实现聚类AP算法(附完整源码)
查看>>
Objective-C实现聚类基本K均值算法(附完整源码)
查看>>
Objective-C实现自动查找和检索加密密钥算法(附完整源码)
查看>>
Objective-C实现自动锁 (附完整源码)
查看>>
Objective-C实现自旋锁(附完整源码)
查看>>
Objective-C实现英文词频统计(附完整源码)
查看>>
Objective-C实现莫尔斯电码算法(附完整源码)
查看>>
Objective-C实现莱布尼兹级数求解π的近似值(附完整源码)
查看>>
Objective-C实现获取 Collatz 序列长度算法(附完整源码)
查看>>
Objective-C实现获取CPU温度(附完整源码)
查看>>
Objective-C实现获取daily horoscope每日星座运势算法(附完整源码)
查看>>
Objective-C实现获取GPU显卡信息(附完整源码)
查看>>
Objective-C实现获取HID设备列表 (附完整源码)
查看>>
Objective-C实现获取PE文件特征(附完整源码)
查看>>
Objective-C实现获取动态库导出函数列表(附完整源码)
查看>>
Objective-C实现获取完整路径下的文件名及后缀(附完整源码)
查看>>
Objective-C实现获取文件大小(附完整源码)
查看>>
Objective-C实现获取文件大小(字节数) (附完整源码)
查看>>
Objective-C实现获取文件头的50个字符(附完整源码)
查看>>
Objective-C实现获取文件夹内所有文件的绝对路径(附完整源码)
查看>>