C++文件读写常用操作整理

C++对于文件的操作需要包含<fstream>头文件

文件类型分为两种:

  1. 文本文件-文件以文件的ASCII码的形式存储在计算机中
  2. 二进制文件-文件以文本的二进制形式存储在计算机中,用户一般不能直接读懂它们

操作文件的三大类:

  1. ofstream: 写操作
  2. ifstream: 读操作
  3. fstream: 读写操作

一、文本文件

1. 写文件

  1. 包含头文件
  2. 创建流对象
  3. 打开文件
  4. 写数据
  5. 关闭文件

注意:文件打开方式可以配合使用,利用 | 操作符

例如:用二进制方式写文件:ios::binary | ios::out

这里说明一下,打开文件的时候下面两种方式都是可以的:

    // 创建流对象
    ofstream ofs;
    // 指定文件名和打开方式
    ofs.open("output.txt", ios::out);  // 这里如果文件不存在会自动创建

   // 创建流对象,指定文件名和打开方式
    ofstream ofs("output.txt", ios::out);
#include <iostream>
#include <fstream>
using namespace std;

int main() {
    // 创建流对象
    ofstream ofs;

    // 指定文件名和打开方式
    ofs.open("output.txt", ios::out);  // 这里如果文件不存在会自动创建

    // 写内容
    ofs << "Hello World" << endl;

    ofs.close();
    return 0;
}

2. 读文件

  1. 包含头文件

  2. 创建流对象

  3. 打开文件并判断文件是否打开成功

    ifstreamis_open()方法可以判断文件是否打开成功

  4. 写数据

  5. 关闭文件

读取上一步的output.txt 文件

方式一:

遇到空格会认为一次读取结束

输出结果:

Hello
World
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    // 创建流对象
    ifstream ifs;

    // 打开文件并判断是否打开成功
    ifs.open("output.txt", ios::in);

    // 文件打开失败一般来说就是路径写错了或者文件名错了
    if(!ifs.is_open()) {
        cout << "文件打开失败" << endl;
        return 1;
    }

    // 读数据
    // 方式一
    char buf[1024] = {};
    // 这种方式遇到空格也会停止
    while(ifs >> buf) {
        cout << buf << endl;
    }

    ifs.close();
    return 0;
}

方式二:

输出结果:

Hello World
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    // 创建流对象
    ifstream ifs;

    // 打开文件并判断是否打开成功
    ifs.open("output.txt", ios::in);

    // 文件打开失败一般来说就是路径写错了或者文件名错了
    if(!ifs.is_open()) {
        cout << "文件打开失败" << endl;
        return 1;
    }

    // 方式二
    char buf[1024] = {};
    while(ifs.getline(buf, sizeof(buf))) {
        cout << buf << endl;
    }

    ifs.close();
    return 0;
}

方式三:

读到string

输出结果:

Hello World
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    // 创建流对象
    ifstream ifs;

    // 打开文件并判断是否打开成功
    ifs.open("output.txt", ios::in);

    // 文件打开失败一般来说就是路径写错了或者文件名错了
    if(!ifs.is_open()) {
        cout << "文件打开失败" << endl;
        return 1;
    }

    // 读数据
    // 方式三
    string buf;
    while(getline(ifs, buf)) {
        cout << buf << endl;
    }

    ifs.close();
    return 0;
}

方式四:不推荐

一个字符一个字符读

输出结果:

Hello World
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    // 创建流对象
    ifstream ifs;

    // 打开文件并判断是否打开成功
    ifs.open("output.txt", ios::in);

    // 文件打开失败一般来说就是路径写错了或者文件名错了
    if(!ifs.is_open()) {
        cout << "文件打开失败" << endl;
        return 1;
    }

    // 读数据
    // 方式四
    char ch;
    while((ch = ifs.get()) != -1) {
        cout << ch;
    }

    ifs.close();
    return 0;
}

二、二进制文件

以二进制的方式对文件进行读写操作

打开方式指定为ios::binary

1. 写文件

二进制方式写文件主要利用流对象调用成员函数write

函数原型:ostream& write(const char * buffer, int len);

字符指针buffer指向内存中的一段存储空间,len是读写的字节数

可以操作内置的数据类型,也可以操作自定义的数据类型

注意:写字符串的时候尽量不要用string,可能会出现一些问题,最好是用char[]

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class Person {
public:
    char name[64];
    int age;
};

int main() {
    // 创建流对象
    ofstream ofs("person.txt", ios::out | ios::binary);
    // 写文件
    Person person = { "张三", 20 };
    ofs.write((const char *)&person, sizeof(Person));

    // 关闭
    ofs.close();
    
    return 0;
}

2. 读文件:

二进制方式读文件主要利用流对象调用成员函数read

函数原型:istream& read(char *buffer, int len);

字符指针buffer指向内存中的一段存储空间,len是读写的字节数

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class Person {
public:
    char name[64];
    int age;
};

int main() {
    // 创建流对象与打开文件并判断文件是否打开成功
    ifstream ifs("person.txt", ios::in | ios::binary);

    if(!ifs.is_open()) {
        cout << "文件打开失败" << endl;
        return 1;
    }

    // 读文件
    Person p;
    ifs.read((char *)&p, sizeof(Person));

    cout << p.name << " " << p.age << endl;

    // 关闭
    ifs.close();
    
    return 0;
}
posted @ 2023-05-01 21:47  junlin623  阅读(138)  评论(0编辑  收藏  举报