博客
关于我
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实现使用数组实现约瑟夫环(附完整源码)
查看>>
Objective-C实现使用管道重定向进程输入输出(附完整源码)
查看>>
Objective-C实现倒计时(附完整源码)
查看>>
Objective-C实现借记款项功能(附完整源码)
查看>>
Objective-C实现八进制转十进制算法(附完整源码)
查看>>
Objective-C实现关系矩阵A和B的乘积(附完整源码)
查看>>
Objective-C实现关系矩阵乘法(附完整源码)
查看>>
Objective-C实现关系矩阵乘法(附完整源码)
查看>>
Objective-C实现关键字移位字母表密码算法(附完整源码)
查看>>
Objective-C实现内存映射文件(附完整源码)
查看>>
Objective-C实现内存泄露检查(附完整源码)
查看>>
Objective-C实现内格尔·施雷肯伯格算法(附完整源码)
查看>>
Objective-C实现几何级数的总和算法 (附完整源码)
查看>>
Objective-C实现凸多边形的凸包问题算法(附完整源码)
查看>>
Objective-C实现分块查找算法(附完整源码)
查看>>
Objective-C实现分块查找算法(附完整源码)
查看>>
Objective-C实现分水岭算法(附完整源码)
查看>>
Objective-C实现分解质因数(附完整源码)
查看>>
Objective-C实现切换数字的符号switchSign算法(附完整源码)
查看>>
Objective-C实现列主元高斯消去法(附完整源码)
查看>>