• C++ 中的文件读写


    • 所有的读写操作五板斧:
    • 1、包含头文件
    • 2、创建流对象
    • 3、通过流对象指定打开方式
    • 4、读写内容
    • 5、关闭文件

    字符模式读写文件:

    写文件

    • 1、包含头文件
    • 2、创建流对象 ofstream ofs;
    • 3、通过流对象指定打开方式:ofs.open(“test.txt”, ios::out);
    • 4、写内容:ofs << “姓名:张三”<< endl;
    • 5、关闭文件: ofs.close();
      具体操作如下:
    #include
    using namespace std;
    #include
    #include 
    
    int main()
    {
    	//1.包含头文件
    
    	//2.创建流对象
    	ofstream ofs;
    
    	//3.指定打开方式
    	ofs.open("test.txt", ios::out);
    
    	//4.写内容
    	ofs << "姓名:张三" << endl;
    	ofs << "性别:男" << endl;
    
    	//5.关闭文件
    	ofs.close(); 
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    读文件

    • 1、包含头文件
    • 2、创建流对象 ifstream ifs;
    • 3、通过流对象指定打开方式
    ifs.open("test.txt", ios::in);
    //判断是否打开成功
    if (!ifs.is_open())
    {
    	cout << "文件打开失败" << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 4、读取内容的四种方式

    单个字符:

    char buf[1024] = { 0 };
    while (ifs >> buf)
    {
    	cout << buf << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    整行字符:

    char buf[1024] = { 0 };
    while (ifs.getline(buf, sizeof(buf)))
    {
    	cout << buf << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    string类型整行读取:

    string buf;
    while (getline(ifs, buf))
    {
    	cout << buf << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    C语言风格单个字符读取:

    char c;
    while ((c = ifs.get()) != EOF)
    {
    	cout << c;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 5、关闭文件: ifs.close();

    二进制读写文件

    写文件

    • 1、包含头文件

    • 2、创建流对象 ofstream ofs;

    • 3、通过流对象指定打开方式:ofs.open(“test.txt”,ios::binary | ios::out);(这里必须同时包含二进制打开方式(binary)和输出模式(out))。
      注:因为有内部构造函数,所以上面两个步骤可合二为一:
      ofstream ofs(“test.txt”, ios::binary | ios::out);

    • 4、写内容:Base p = { 23, “gzy” };
      ofs.write((const char*)&p, sizeof(Base));
      注:这里的Base p 是一个类实例化的对象,它包含int age 和 char name[] 两个属性,二进制写的操作实际是将目标内容 逐位写入 目标文件,所以需要传入写入位置的指针,和需要写入的字节大小。

    • 5、关闭文件: ofs.close();

    class Base
    {
    public:
    	int age;
    	char name[1024];
    };
    
    int main()
    {
    	ofstream ofs;
    	ofs.open("text.txt", ios::binary | ios::out);
    	//有构造函数,可以写 ofstream ofs("text.txt", ios::binary | ios::out);
    	Base p = { 23, "振远" };
    	ofs.write((const char*)&p, sizeof(Base));
    	ofs.close();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    读文件

    • 1、包含头文件
    • 2、创建流对象 :ifstream ifs;
    • 3、通过流对象指定打开方式
    ifs.open("text.txt", ios::binary | ios::in);
    if (!ifs.is_open())
    {
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 4、读写内容
    Base p1;
    ifs.read((char*)&p1, sizeof(Base));
    cout << p1.age << p1.name << endl;
    
    • 1
    • 2
    • 3
    • 5、关闭文件 ifs.close();
  • 相关阅读:
    Java基础
    Redis最全详解(三)——SpringBoot整合2种方式
    Svn常见问题分析及解决方案
    Stable Diffusion扩散模型推导公式的基础知识
    字符串压缩(二)之LZ4
    .net 温故知新【17】:Asp.Net Core WebAPI 中间件
    labelimg标注的VOC格式标签xml文件和yolo格式标签txt文件相互转换
    界面拖动界面拖动
    Leecode第13题题解
    MySQL——数据库基础
  • 原文地址:https://blog.csdn.net/KamikazePilot/article/details/126690852