samedi 1 octobre 2016

Using istream_iterator on an fstream

The following compiles and executes without error:

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

int main()
{
    std::fstream file("somefile.x",std::ios::in|std::ios::out|std::ios::trunc);
    file << "drop some fstream text" << std::flush;
    std::string stringOfFile(std::istream_iterator<char>(file),{});
    std::cout << stringOfFile << std::flush;

    std::stringstream strStream;
    strStream << "drop some strStream text" << std::flush;
    std::string stringOfStream(std::istream_iterator<char>(strStream),{});
    std::cout << stringOfStream << std::flush;
}

However the output is:

dropsomestrStreamtext

and the contents of the file are not output. The file does contain the text drop some fstream text; so I figure the issue is with my use of the istream_iterator with an fstream.

Aucun commentaire:

Enregistrer un commentaire