C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > C++ std::ifstream使用

C++中std::ifstream的使用方法介绍

作者:赛先生.AI

这篇文章主要给大家介绍了关于C++中std::ifstream使用方法的相关资料,std::ifstream 是输入文件流类,用于从文件中读取数据,文中通过代码介绍的非常详细,需要的朋友可以参考下

1. 文件流

为了便于对文件的操作,C++标准库中提供了文件输入输出流fstream,并提供了<fstream>头文件。

fstream又细分了两个分支,分别是处理输入文件流的ifstream和处理输出文件流的ofstream。

ifstream负责将文件从硬盘读取至内存中。

ofstream负责将文件从内存写入硬盘中。

这两者所处理的文件流均包含二进制文件(binary)和文本文件(text)。

接下来我们将针对输入文件流ifstream用实际的例程介绍其使用方法。

2. 读取文本

(1)按行读取文本

#include <fstream>
#include <iostream>
#include <string>

int main() {
    std::string line;
    std::ifstream file("example.txt"); // 打开文本文件
    if (file.is_open()) {
        while (getline(file, line)) { // 按行读取
            std::cout << line << '\n';
        }
        file.close();
    } else {
        std::cout << "Unable to open file";
    }
    return 0;
}

(2)按字符读取文本

#include <fstream>
#include <iostream>
#include <string>

int main() {
    std::ifstream file("example.txt"); // 假设有一个名为example.txt的文件
    if (!file.is_open()) {
        std::cerr << "Error opening file" << std::endl;
        return 1;
    }
    char ch;
    while (file.get(ch)) { // 每次读取一个字符,直到文件末尾
        std::cout << ch;
    }
    file.close(); // 关闭文件
    return 0;
}

(3)逐个字符串读取文本

#include <fstream>
#include <iostream>
#include <string>

int main() {
    std::ifstream file("example.txt");
    std::string word;
    /// 默认会忽略输入流中的空白字符,包括空格、制表符、换行符等
    while (file >> word) { // 按空白字符分隔读取每个单词
        std::cout << word << std::endl;
    }
    file.close();
    return 0;
}

(4)逐个数字读取文本(适用于文本中存在某类型的数字)

#include <fstream>
#include <iostream>

int main() {
    std::ifstream file("numbers.txt");
    int number;
    /// 默认会忽略输入流中的空白字符,包括空格、制表符、换行符等
    while (file >> number) { // 从文件中读取整数
        std::cout << number << std::endl;
    }
    file.close();
    return 0;
}

(5)读取整个文件到字符串中

#include <fstream>
#include <sstream>
#include <iostream>
#include <string>

int main() {
    std::ifstream file("example.txt");
    std::stringstream buffer;
    buffer << file.rdbuf(); // 读取整个文件内容到buffer
    std::string contents = buffer.str(); // 将读取的内容转换为字符串
    std::cout << contents;
    file.close();
    return 0;
}

3. 读取二进制

(1)读取整个二进制文件

#include <fstream>
#include <iostream>
#include <vector>

int main() {
    // 以二进制模式打开文件
    std::ifstream file("example.bin", std::ios::binary | std::ios::ate); // std::ios::ate 定位到文件末尾,便于获取文件大小
    if (file.is_open()) {
        // 获取文件大小
        std::streamsize size = file.tellg();
        file.seekg(0, std::ios::beg); // 定位回文件开始
        // 分配内存缓冲区
        std::vector<char> buffer(size);
        // 读取文件到缓冲区
        if (file.read(buffer.data(), size)) {
            /* 成功读取后的处理 */
        } else {
            std::cout << "Error reading file";
        }
        file.close();
    } else {
        std::cout << "Unable to open file";
    }
    return 0;
}

(2)分段读取二进制文件

#include <fstream>
#include <iostream>
#include <vector>

int main() {
    // 打开一个二进制文件
    std::ifstream file("example.bin", std::ios::binary);
    if (!file) {
        std::cerr << "Unable to open file" << std::endl;
        return 1;
    }
    const size_t bufferSize = 1024; // 定义每次读取的字节数
    char buffer[bufferSize]; // 创建一个缓冲区
    // 循环读取文件,直到到达文件末尾
    while (!file.eof()) {
        file.read(buffer, bufferSize); // 尝试读取下一段数据
        std::streamsize bytes = file.gcount(); // 获取实际读取的字节数
        /// 处理读取到的数据...
        /// 这里只是简单地打印出实际读取的字节数
        std::cout << "Read " << bytes << " bytes" << std::endl;
        /// 如果需要,可以对buffer中的数据进行处理
        /// 判断退出条件(此处可避免多读一次)。
        if(file.peek() == EOF){
            break;
        }
    }
    file.close(); // 关闭文件
    return 0;
}

附:std::ifstream 和std::ofstream 的异同点

std::ifstream 和 std::ofstream 都是C++标准库中的文件流类,用于进行文件的输入和输出操作。

异同点如下:

相同点:

都是用于文件操作的流类。

都需要通过文件路径来创建文件流对象。

都可以使用相同的成员函数来进行文件的读写操作。

不同点:

std::ifstream 是输入文件流类,用于从文件中读取数据。

std::ofstream 是输出文件流类,用于向文件中写入数据。

std::ifstream 对象创建时需要指定文件路径,并且只能进行读取操作。

std::ofstream 对象创建时需要指定文件路径,并且只能进行写入操作。

std::ifstream 对象默认以只读方式打开文件,不能对文件进行写入操作。

std::ofstream 对象默认以写入方式打开文件,不能对文件进行读取操作。

std::ifstream 对象可以使用 >> 运算符来从文件中读取数据。

std::ofstream 对象可以使用 << 运算符来将数据写入文件。

总结 

到此这篇关于C++中std::ifstream使用方法的文章就介绍到这了,更多相关C++ std::ifstream使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

阅读全文