ifstream not reading EOF character

First thing is first, you shouldn’t check like that. eof() doesn’t return true until after a failed read. But you can do better (and easier)! check the stream state with the implicit conversion to void* which can be used in a bool context. Since most of the read operations on streams return a reference to … Read more

How to read huge file in c++

There are a couple of things that you can do. First, there’s no problem opening a file that is larger than the amount of RAM that you have. What you won’t be able to do is copy the whole file live into your memory. The best thing would be for you to find a way … Read more

Replace a line in text file

I’m afraid you’ll probably have to rewrite the entire file. Here is how you could do it: #include <iostream> #include <fstream> using namespace std; int main() { string strReplace = “HELLO”; string strNew = “GOODBYE”; ifstream filein(“filein.txt”); //File to read from ofstream fileout(“fileout.txt”); //Temporary file if(!filein || !fileout) { cout << “Error opening files!” << … 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

C++ ifstream failbit and badbit

According to cplusplus.com: failbit is generally set by an input operation when the error was related to the internal logic of the operation itself, so other operations on the stream may be possible. While badbit is generally set when the error involves the loss of integrity of the stream, which is likely to persist even … Read more