Why can’t I move std::ofstream?

According to the standard 27.9.1.11 basic_ofstream constructors or, its more “readable” version http://en.cppreference.com/w/cpp/io/basic_ofstream/basic_ofstream , std::basic_ostream<> has a move constructor, so the code should compile. clang++ 3.5 compiles it with -std=c++11 or -std=c++1y. Also gcc5 compiles it, so probably it is not implemented in libstdc++ for gcc < 5 Interestingly, the lack of move semantics is … Read more

How can I use non-default delimiters when reading a text file with std::fstream?

An istream treats “white space” as delimiters. It uses a locale to tell it what characters are white space. A locale, in turn, includes a ctype facet that classifies character types. Such a facet could look something like this: #include <locale> #include <iostream> #include <algorithm> #include <iterator> #include <vector> #include <sstream> class my_ctype : public … Read more

Reading line from text file and putting the strings into a vector?

Simplest form: std::string line; std::vector<std::string> myLines; while (std::getline(myfile, line)) { myLines.push_back(line); } No need for crazy c thingies 🙂 Edit: #include <iostream> #include <fstream> #include <string> #include <vector> int main() { std::string line; std::vector<std::string> DataArray; std::vector<std::string> QueryArray; std::ifstream myfile(“OHenry.txt”); std::ifstream qfile(“queries.txt”); if(!myfile) //Always test the file open. { std::cout<<“Error opening output file”<< std::endl; system(“pause”); return … Read more

How to truncate a file while it is open with fstream

I don’t think you can get “atomic” operation but using the Filesystem Technical Specification that has now been accepted as part of the Standard Library (C++17) you can resize the file like this: #include <fstream> #include <sstream> #include <iostream> #include <experimental/filesystem> // compilers that support the TS // #include <filesystem> // C++17 compilers // for … Read more