【问题标题】:Empty file when using boost::filtering_streambuf with newline_filter将 boost::filtering_streambuf 与 newline_filter 一起使用时为空文件
【发布时间】:2020-02-18 16:09:27
【问题描述】:

我希望使用boost::iostreams::filtering_streambuf 和newline_filter 将一条数据写入通过std::fopen 打开的文件。

这是一个我一直在尝试使用的小型可重现测试用例。

它只是产生一个空文件。

#include <string>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <errno.h>

#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/filter/newline.hpp>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <iosfwd>
#include <string>
#include <boost/iostreams/flush.hpp>
#include <boost/iostreams/operations.hpp>
#include <fstream>
#include <cstdio>

int main()
{
    FILE *fp = nullptr;  

    std::string d ("file");

    fp = std::fopen(d.c_str(), "w");
    const int fd = fileno(fp);

    boost::iostreams::file_descriptor_sink output(fd, boost::iostreams::never_close_handle);
    boost::iostreams::filtering_streambuf<boost::iostreams::output>obuf;

#if defined(_WIN32)
    obuf.push(boost::iostreams::newline_filter(boost::iostreams::newline::dos));
#else
    obuf.push(boost::iostreams::newline_filter(boost::iostreams::newline::mac));
#endif

    obuf.push(output);

    std::ostream buffer(&obuf);

    std::string myteststr = "Hello \n World\n";
    buffer << myteststr;

    buffer.flush();
    boost::iostreams::flush(obuf);

    return 0;
}

我在这里有什么明显的遗漏吗?

【问题讨论】:

    标签: c++ boost iostream boost-iostreams


    【解决方案1】:

    我无法重现这种行为:

    Live On Coliru¹

    这用于编译和执行

    g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp -lboost_{system,iostreams} && ./a.out
    file output.txt
    xxd output.txt
    

    打印

    output.txt: ASCII text, with CRLF line terminators
    00000000: 4865 6c6c 6f20 0d0a 2057 6f72 6c64 0d0a  Hello .. World..
    

    我确实建议添加明确的 fd 关闭,尽管这在技术上应该无关紧要。

    另外,2019 年有什么理由使用FILE* 吗?我假设您将其包括在内是因为遗留代码仅使用它。


    ¹ 列表:

    #include <boost/iostreams/device/file_descriptor.hpp>
    #include <boost/iostreams/filter/newline.hpp>
    #include <boost/iostreams/filtering_streambuf.hpp>
    #include <boost/iostreams/flush.hpp>
    #include <cstdio>
    #include <iostream>
    #include <string>
    
    int main() {
        FILE* fp = std::fopen("output.txt", "w");
        const int fd = fileno(fp);
    
        boost::iostreams::file_descriptor_sink output(fd, boost::iostreams::never_close_handle);
        boost::iostreams::filtering_streambuf<boost::iostreams::output> obuf;
    
        obuf.push(boost::iostreams::newline_filter(boost::iostreams::newline::dos));
        obuf.push(output);
    
        std::ostream buffer(&obuf);
    
        std::string myteststr = "Hello \n World\n";
        buffer << myteststr;
    
        buffer.flush();
        boost::iostreams::flush(obuf);
    
        ::close(fd);
    }
    

    【讨论】:

    • TBH,我意识到我正在使用cat 命令检查文件输出,由于某种未知原因没有打印任何内容.. :P 当我在 Windows 上用记事本打开文件时,我得到了相应平台的正确输出,即Unix(LF) 和Windows(CRLF)。
    猜你喜欢
    • 2011-07-24
    • 1970-01-01
    • 2017-12-27
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 2012-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多