You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ifstream.read()无法读取内容?C++文件读写问题求助

问题:ifstream.read() 无法读取文件内容

我在项目中遇到文件读取问题,编写了如下测试代码尝试用ifstream.read()读取文件,但未读取到任何内容。查阅大量相关问题后仍无法找出原因,恳请帮助。

测试代码:

#include <iostream>
#include <fstream>

int main() {
    /*--------------- write file ----------------*/
    std::ofstream ofs("testfile", std::ios::binary);
    const char *content = "11223344";
    ofs.write(content, sizeof content);

    system("chmod 777 testfile");

    /*--------------- read file ----------------*/

    char arr[8] = {0};
    const int length = sizeof arr / sizeof arr[0];
    std::ifstream ifs("testfile", std::ios::binary);

    std::cout << "good: " << ifs.good() << std::endl;  // 1

    ifs.seekg(0, ifs.end);
    int file_size = ifs.tellg();
    ifs.seekg(0, ifs.beg);
    std::cout << "file_size: " << file_size << std::endl << std::endl;  // 0

    if (ifs) {
        ifs.read(&arr[0], 2);
        std::cout << "after read," << std::endl;
        std::cout << "gcount: " << ifs.gcount() << std::endl;  // 0
        std::cout << "good: " << ifs.good() << std::endl;  // 0
        ifs.read(&arr[2], 2);
        ifs.read(&arr[4], 2);
        ifs.read(&arr[6], 2);
    } else {
        std::cout << "file not found" << std::endl;
    }

    for (int i = 0; i < length; ++i) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

程序输出:

good: 1
file_size: 0

after read,
gcount: 0
good: 0
0 0 0 0 0 0 0 0 

问题根源与修复方案

核心问题出在文件写入阶段,而非读取逻辑:

  1. 写入长度计算错误:sizeof content获取的是指针类型的大小(32位系统为4字节,64位为8字节),而非字符串"11223344"的实际长度。即便指针大小刚好等于字符串长度,后续的缓冲区未刷新问题仍会导致读取失败。

  2. 缓冲区未刷新到磁盘:写入文件后,ofs对象仍处于作用域内(未被析构),且未手动调用close()或flush(),导致写入内容停留在内存缓冲区,并未实际写入磁盘,因此读取时文件为空。

修正后的代码

#include <iostream>
#include <fstream>
#include <cstring>

int main() {
    /*--------------- write file ----------------*/
    std::ofstream ofs("testfile", std::ios::binary);
    const char *content = "11223344";
    // 使用strlen获取字符串实际长度,或直接指定长度8
    ofs.write(content, strlen(content));
    // 手动关闭文件,强制将缓冲区内容写入磁盘
    ofs.close();

    // chmod命令非必需,默认权限已支持读取
    // system("chmod 777 testfile");

    /*--------------- read file ----------------*/

    char arr[8] = {0};
    const int length = sizeof arr / sizeof arr[0];
    std::ifstream ifs("testfile", std::ios::binary);

    std::cout << "good: " << ifs.good() << std::endl;

    ifs.seekg(0, ifs.end);
    int file_size = ifs.tellg();
    ifs.seekg(0, ifs.beg);
    std::cout << "file_size: " << file_size << std::endl << std::endl;

    if (ifs) {
        ifs.read(&arr[0], 2);
        std::cout << "after read," << std::endl;
        std::cout << "gcount: " << ifs.gcount() << std::endl;
        std::cout << "good: " << ifs.good() << std::endl;
        ifs.read(&arr[2], 2);
        ifs.read(&arr[4], 2);
        ifs.read(&arr[6], 2);
    } else {
        std::cout << "file not found" << std::endl;
    }

    // 输出ASCII值
    for (int i = 0; i < length; ++i) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    // 输出字符形式,更直观
    for (int i = 0; i < length; ++i) {
        printf("%c ", arr[i]);
    }
    printf("\n");
}

关键修正点说明

  • 用strlen(content)替代sizeof content,正确获取字符串的实际字节长度。
  • 调用ofs.close()手动关闭文件,确保缓冲区内容立即写入磁盘;若需保持文件打开,可改用ofs.flush()刷新缓冲区。
  • 移除不必要的chmod命令,默认创建的文件已具备读取权限。

内容的提问来源于stack exchange,提问作者Xingx1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 07:17:01