博客
关于我
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实现lorenz transformation 洛伦兹变换算法(附完整源码)
查看>>
Objective-C实现Lower-Upper Decomposition上下分解算法(附完整源码)
查看>>
Objective-C实现LowerCaseConversion小写转换算法(附完整源码)
查看>>
Objective-C实现lowest common ancestor最低共同祖先算法(附完整源码)
查看>>
Objective-C实现LRU 缓存算法(附完整源码)
查看>>
Objective-C实现LRU缓存(附完整源码)
查看>>
Objective-C实现LRU(least recently used)算法(附完整源码)
查看>>
Objective-C实现lstm prediction预测算法(附完整源码)
查看>>
Objective-C实现lucas数列算法(附完整源码)
查看>>
Objective-C实现Luhn (Mod 10)Algorithm算法(附完整源码)
查看>>
Objective-C实现LZW编码(附完整源码)
查看>>
Objective-C实现MAC桌面暗水印(附完整源码)
查看>>
Objective-C实现mandelbrot曼德勃罗特集算法(附完整源码)
查看>>
Objective-C实现markov chain马尔可夫链算法(附完整源码)
查看>>
Objective-C实现MATLAB中Filter函数功能(附完整源码)
查看>>
Objective-C实现matrix chainorder矩阵链顺序算法(附完整源码)
查看>>
Objective-C实现matrix exponentiation矩阵求幂算法(附完整源码)
查看>>
Objective-C实现MatrixMultiplication矩阵乘法算法 (附完整源码)
查看>>
Objective-C实现max non adjacent sum最大非相邻和算法(附完整源码)
查看>>
Objective-C实现max subarray sum最大子数组和算法(附完整源码)
查看>>