博客
关于我
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/

你可能感兴趣的文章
Nginx 我们必须知道的那些事
查看>>
nginx 配置~~~本身就是一个静态资源的服务器
查看>>
Nio ByteBuffer组件读写指针切换原理与常用方法
查看>>
NLP 基于kashgari和BERT实现中文命名实体识别(NER)
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
nullnullHuge Pages
查看>>
Numpy如何使用np.umprod重写range函数中i的python
查看>>
oauth2-shiro 添加 redis 实现版本
查看>>
OAuth2.0_JWT令牌-生成令牌和校验令牌_Spring Security OAuth2.0认证授权---springcloud工作笔记148
查看>>
OAuth2.0_授权服务配置_Spring Security OAuth2.0认证授权---springcloud工作笔记140
查看>>
OAuth2.0_授权服务配置_资源服务测试_Spring Security OAuth2.0认证授权---springcloud工作笔记146
查看>>
OAuth2.0_环境搭建_Spring Security OAuth2.0认证授权---springcloud工作笔记139
查看>>
object detection错误之Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
查看>>
Objective-C享元模式(Flyweight)
查看>>
Objective-C以递归的方式实现二叉搜索树算法(附完整源码)
查看>>
Objective-C实现1000 位斐波那契数算法(附完整源码)
查看>>
Objective-C实现2 个数字之间的算术几何平均值算法(附完整源码)
查看>>
Objective-C实现3n+1猜想(附完整源码)
查看>>
Objective-C实现A-Star算法(附完整源码)
查看>>
Objective-C实现all combinations所有组合算法(附完整源码)
查看>>