c++boostiostreamboost-iostreams

Empty file when using boost::filtering_streambuf with newline_filter


I wish to write a piece of data to the file opened via std::fopen using boost::iostreams::filtering_streambuf with newline_filter.

Here is a small reproducible test case that I have been trying to work with.

It just produces an empty file.

#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;
}

Is there something obvious that I am missing here ?


Solution

  • I can't reproduce that behaviour:

    Live On Coliru¹

    This for the compilation and execution with

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

    Prints

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

    I do suggest to add the explicit fd close, although that should not technically matter.

    Also, is there any reason to use FILE* in 2019? I'm assuming you're including that because of legacy code that uses it only.


    ¹ Listing:

    #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);
    }