C++——fstream文件读写操作

本文详细介绍了C++中的文本文件和二进制文件操作,包括ofstream、ifstream和fstream的使用,以及不同文件打开方式如读写、追加和二进制模式。还提供了文本文件和包含Person类的二进制文件的读写代码示例。
摘要由CSDN通过智能技术生成

文件类型

  • 文本文件 - 文件以文本的ASCII码形式存储在计算机中

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

操作文件类

  • ofstream:写操作

  • ifstream: 读操作

  • fstream : 读写操作

文件打开方式

打开方式解释
ios::in为读文件而打开文件
ios::out为写文件而打开文件
ios::ate初始位置:文件尾
ios::app追加方式写文件
ios::trunc如果文件存在先删除,再创建
ios::binary二进制方式

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

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

文本文件读写代码示例

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

int write_file(string file_path,string file_content){
    ofstream ofs;
    ofs.open(file_path,ios::out);
    ofs<<file_content<<endl;
    ofs.close();
    return 1;
}

void read_file(string file_path){
    ifstream ifs;
    ifs.open(file_path,ios::in);
    if (!ifs.is_open()) {
        cout<<"file can't open"<<endl;
    }
    string buffer;
    while (getline(ifs,buffer)) { //get by line
        cout<<buffer<<endl;
    }
    ifs.close();
}

int main(){
    write_file("./test.txt","Hello World!\nHello Sophia!\nHello Anna!\nYumy!");
    read_file("./test.txt");
    return 1;
}

二进制文件读写代码示例

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

// definded class
class Person{
public:
    char m_name[64];
    int m_age;
};

// write binary file
void write_binary_file(string file_path){
    // create ofs obj
    ofstream ofs(file_path,ios::out|ios::binary);
    //ofstream ofs;
    // open file
    //ofs.open(file_path,ios::binary);
    Person p = {"Sophia",20};
    // write
    ofs.write((const char *)&p, sizeof(p));
    // close file
    ofs.close();
}

Person p_read;
void read_binary_file(string file_path){
    // create stream aoj
    ifstream ifs(file_path,ios::in|ios::binary);
    // read
    ifs.read((char *)&p_read, sizeof(Person));
    // cout
    cout<<p_read.m_name<<"\n"<<p_read.m_age<<endl;
    // close
    ifs.close();
}

int main(){
    string file_path="./test1.txt";
    write_binary_file(file_path);
    read_binary_file(file_path);
    return 1;
}

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`std::fstream` 是 C++ 标准库中的一个类,用于进行文件操作。它可以操作文本文件和二进制文件,提供了与 `std::iostream` 相似的接口,可以通过 `>>` 和 `<<` 运算符进行输入输出操作。 下面是一些常用的 `std::fstream` 操作: 1. 打开文件 在使用 `std::fstream` 进行文件操作之前,需要先打开文件。可以使用 `open` 函数打开文件,如下所示: ```c++ #include <fstream> std::fstream file; file.open("example.txt", std::ios::in); ``` 上述代码中,我们使用 `open` 函数打开了一个名为 `example.txt` 的文件,并指定了文件的打开方式为取模式。文件打开成功后,我们就可以对其进行操作了。 2. 文件 可以使用 `<<` 运算符向文件入数据,如下所示: ```c++ file << "Hello, world!"; ``` 上述代码中,我们使用 `<<` 运算符向文件入了一个字符串。 3. 文件 可以使用 `>>` 运算符从文件取数据,如下所示: ```c++ std::string str; file >> str; ``` 上述代码中,我们使用 `>>` 运算符从文件取了一个字符串。 4. 关闭文件 使用完文件后,需要调用 `close` 函数来关闭文件,如下所示: ```c++ file.close(); ``` 上述代码中,我们调用 `close` 函数关闭了刚才打开的文件。 5. 判断文件是否打开 在进行文件操作之前,可以使用 `is_open` 函数判断文件是否打开成功,如下所示: ```c++ if (file.is_open()) { // 文件打开成功 } else { // 文件打开失败 } ``` 上述代码中,我们使用 `is_open` 函数判断文件是否打开成功。 除此之外,`std::fstream` 还提供了一些其他的操作,如定位文件指针、取二进制数据等。具体的操作可以参考 C++ 的官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值