zoukankan      html  css  js  c++  java
  • [C/C++基础]读写文件

    1.打开、关闭文件:

    FILE* fp = fopen(string.c_str(), FLAG);
    • string.c_str():需用C语言字符串形式;
    • FLAG说明:
      • r: 只读方式打开;
      • w: 只写方式打开;
      • a:追加方式打开;
      • r+: 以读/写方式打开;(无文件:出错)
      • w+: 以读/写方式打开;(无文件:新建)
    int fclose(FILE* fp);
    • 成功:返回0;
    • 失败:返回EOF;
    2.读写文件:
    char *fgets(char *s, int n, FILE *stream); 
    
    • 从流中读取n-1个字符,除非读完一行,参数s是来接收字符串,如果成功则返回s的指针,否则返回NULL

    3.文件尾的测试:  int feof(FILE *fp); 
    //当前已到达文件尾返回非0, 否则返回0


    使用fstream方式:

      1.包括头文件:

    #include <fstream>
    ofstream         //文件写操作 内存写入存储设备 
    ifstream         //文件读操作,存储设备读区到内存中
    fstream          //读写操作,对打开的文件可进行读写操作 
    

      2.打开文件:

    void open ( const char * filename, ios_base::openmode mode = ios_base::in | ios_base::out );
    
    void open(const wchar_t *_Filename, ios_base::openmode mode= ios_base::in | ios_base::out,
            int prot = ios_base::_Openprot);
    

      

  • 相关阅读:
    python面试
    Python 3.x--使用re模块,实现计算器运算实例
    Python 3.x--模块导入
    Python 3.x--序列化及反序列化
    Python 3.x--装饰器
    Python 3.x--函数的参数问题
    Python 3.x--文件seek、tell的用法
    Python 3.x--字典循环
    Python3.x--文件读写与list
    Python简介
  • 原文地址:https://www.cnblogs.com/imagezy/p/5233854.html
Copyright © 2011-2022 走看看