fstream、ifstream、ofstream和stringstream


ifstream
ifstream 是 C++ 标准库中用于文件读取的输入流类之一,用于从文件中读取数据。可以通过打开文件并将其与 ifstream 对象绑定,然后使用输入操作符 >> 从文件中读取数据。

代码示例:

#include <iostream>
#include <fstream>
#include <string>
 
int main() {
    std::ifstream file("example.txt");//获取文件对象
    //如果文件不存在
    if (!file) {
        std::cout << "File not found!" << std::endl;
        return 1;
    }
    //使用getline来读取文件
    std::string line;
    while (std::getline(file, line)) {
        std::cout << line << std::endl;
    }
 
    file.close();
    return 0;
}
ofstream 
ofstream是C++标准库中的一个输出文件流,用于将数据写入文件。与ifstream类似,需要先创建一个ofstream对象并指定文件名及打开模式。

代码示例:

#include <fstream>
 
using namespace std;
 
int main()
{
    ofstream ofs("example.txt", ios::out | ios::binary);//以输出和二进制的方式打开文件
    if (ofs.is_open())
    {
        ofs << "This is a test." << endl;//写入文件
        ofs.close();
    }
    else
    {
        cout << "Failed to open file." << endl;
    }
    
    return 0;
}
除了 ios::out 和 ios::binary 之外,常见的文件打开模式还包括:
ios::in:以只读方式打开文件。
ios::trunc:如果文件存在,删除其中所有的内容。如果文件不存在,创建一个新文件。
ios::app:在文件末尾追加内容,而不是覆盖已有内容。
ios::ate:在打开文件时将文件指针移到文件末尾。
ios::in | ios::out:以读写方式打开文件。
ios::in | ios::binary:以二进制模式打开文件并以只读方式访问。
这些模式可以通过按位或 | 连接在一起使用,以便打开文件时同时具有多个模式。例如,ios::in | ios::binary 可以以只读的二进制模式打开文件。
fstream
fstream是C++中用来读写文件的类,它可以用来打开文件、读取文件、写入文件、关闭文件等操作。fstream类继承了ifstream和ofstream类的所有特性,因此可以用来读取和写入文件。

使用fstream类进行文件操作时,需要指定文件的打开模式,常见的模式有:

ios::in:读模式,打开文件用于读取。
ios::out:写模式,打开文件用于写入。
ios::binary:二进制模式,以二进制形式打开文件,不会自动将换行符转换为\n。
ios::app:追加模式,打开文件并在文件末尾追加内容。
ios::ate:定位到文件末尾,打开文件时将文件指针移动到文件末尾。
ios::trunc:截断文件,打开文件时将清空文件内容,只允许写入。
代码示例: 

#include <iostream>
#include <fstream>
#include <string>
 
int main() {
    // 写入文件
    std::fstream outfile("example.txt", std::ios::out | std::ios::binary);
    if (outfile.is_open()) {
        std::string message = "Hello, World!";
 
        //write函数以C语言字符串形式写入
        outfile.write(message.c_str(), message.size());
        outfile.close();
    }
    else {
        std::cout << "Failed to open file for writing." << std::endl;
        return 1;
    }
 
    // 读取文件
    std::fstream infile("example.txt", std::ios::in | std::ios::binary);
    if (infile.is_open()) {
        // 获取文件长度
        infile.seekg(0, infile.end);
        int length = infile.tellg();
        infile.seekg(0, infile.beg);
 
        // 读取文件内容
        char* buffer = new char[length];
        infile.read(buffer, length);
        infile.close();
 
        std::string message(buffer, length);
        delete[] buffer;
 
        std::cout << "File content: " << message << std::endl;
    }
    else {
        std::cout << "Failed to open file for reading." << std::endl;
        return 1;
    }
 
    return 0;
}
 
seekg() 函数是 C++ 标准库中 istream 类的一个成员函数,用于设置读取文件的位置。
其中 g 表示 get,代表输入流,因此 seekg 用于设置输入文件的读取位置。
 
该函数有两个参数,第一个参数是需要定位到的位置,第二个参数是指定相对位置的参数。
第一个参数可以是一个整数,代表要跳过的字节数或要定位的位置。第二个参数可以是
 ios::beg、ios::cur、ios::end 中的一个,分别代表相对于文件开头、当前位置和
文件结尾的位置。
 
tellg() 获取当前读取位置的偏移量,这个偏移量通常用于之后重新定位到该位置进行
读取。在文件读取完成后,使用 tellg() 可以获得文件的总大小。
 
需要注意的是,当文件读取位置在文件结尾时,tellg() 返回的值为-1。如果需要判断
当前是否已到达文件结尾,可以先调用 eof() 函数检查文件是否已结束。
 stringstream
std::stringstream 是 C++ 标准库中的一个类,用于对字符串进行输入输出操作。它可以将字符串视为一个 I/O 流,并提供了与标准 I/O 流类似的输入输出操作。

代码示例:

#include <iostream>
#include <sstream>
 
int main() {
    // 将多种类型的数据格式化为字符串
    int i = 42;
    double d = 3.1415926;
    std::string s = "Hello, World!";
    
    std::stringstream ss;
    ss << "i = " << i << ", d = " << d << ", s = " << s;
    std::string formatted = ss.str();
    
    std::cout << formatted << std::endl; // 输出 "i = 42, d = 3.14159, s = Hello, World!"
    
    // 从字符串中解析出不同类型的数据
    std::string input = "12 3.45 abc";
    int a;
    double b;
    std::string c;
    
    std::stringstream ss2(input);
    ss2 >> a >> b >> c;
    
    std::cout << a << ", " << b << ", " << c << std::endl; // 输出 "12, 3.45, abc"
    
    return 0;

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值