使用C++通过输入重定向读取二进制文件的最佳方法

6

我正在尝试在运行时通过输入重定向(stdin)读取一个大型二进制文件,并且 stdin 是强制要求的。

./a.out < input.bin

目前我使用的是fgets。但是fgets会跳过空格和换行符。我想同时包括它们。我的currentBuffersize大小可能会动态变化。

FILE * inputFileStream = stdin; 
int currentPos = INIT_BUFFER_SIZE;
int currentBufferSize = 24; // opt
unsigned short int count = 0; // As Max number of packets 30,000/65,536
while (!feof(inputFileStream)) {
    char buf[INIT_BUFFER_SIZE]; // size of byte
    fgets(buf, sizeof(buf), inputFileStream);
    cout<<buf;
    cout<<endl;
}

感谢您的提前帮助。

3
fread 用于未经处理的输入。而 fgets 则进行文本处理。不要忽略返回值,它是存储到缓冲区的有效记录数。 - Ben Voigt
1
你为什么在C++中使用C stdio? - Barmar
@DOUGLASO.MOEN 是的,你可以。首先,那个问题是关于使用 cin 进行操作的,所以它与这个问题没有太大关系;其次,那里的答案实际上告诉了你如何做。这个问题只是因为没有阅读 stdio 文档 / 没有在 Google 上搜索 "从 stdin 读取二进制数据" 而导致的失败。 - Jason C
2
@JasonC ... 谢谢,我第一次阅读时没有区分stdin和std :: cin。 - 2785528
显示剩余5条评论
1个回答

13

如果是我,我可能会做类似于这样的事情:

const std::size_t INIT_BUFFER_SIZE = 1024;

int main()
{
    try
    {
        // on some systems you may need to reopen stdin in binary mode
        // this is supposed to be reasonably portable
        std::freopen(nullptr, "rb", stdin);

        if(std::ferror(stdin))
            throw std::runtime_error(std::strerror(errno));

        std::size_t len;
        std::array<char, INIT_BUFFER_SIZE> buf;

        // somewhere to store the data
        std::vector<char> input;

        // use std::fread and remember to only use as many bytes as are returned
        // according to len
        while((len = std::fread(buf.data(), sizeof(buf[0]), buf.size(), stdin)) > 0)
        {
            // whoopsie
            if(std::ferror(stdin) && !std::feof(stdin))
                throw std::runtime_error(std::strerror(errno));

            // use {buf.data(), buf.data() + len} here
            input.insert(input.end(), buf.data(), buf.data() + len); // append to vector
        }

        // use input vector here
    }
    catch(std::exception const& e)
    {
        std::cerr << e.what() << '\n';
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

请注意,您可能需要以二进制模式重新打开stdin,不确定该方法的可移植性,但各种文档表明在各种系统中得到了合理的支持。


哦...我懂了...fread应该也可以读取空格和换行符,但出于某种原因它跳过了它们。 - Jerry
@Jerry,如果你按照我的示例以二进制模式freopen,它不应该跳过它们。如果它仍然跳过它们,我建议你编辑你的问题并发布一个完整的代码示例,以便描述你遇到的错误。 - Galik
它正在使用fwrite工作。你能告诉我如何将缓冲区内容附加到数组或字符串中吗?如果我尝试打印缓冲区,则循环不会终止,并且在强制终止时会打印当前缓冲区值... - Jerry
@Jerry 我修改了这个例子,将每个接收到的数据缓冲区附加到名为 inputstd::vector 中。基本上,每次循环时读取的数据从 buf.data() 开始,到 buf.data() + len 结束。 - Galik
非常感谢,它起作用了 :) .. 我使用了char[],因为我无法弄清array<char,INIT_BUFFER_SIZE> buf; - Jerry
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接