It was planned that this part of the code will open the file, enumerate and output its contents in rows, but in fact the entire contents of the file are output to the console at once. According to the description on the site http://www.cplusplus.com/reference/istream/istream/get/ in this case, get () should read before the newline character. What is the problem?

std::ifstream is; is.open(file.txt); std::streambuf *buf = is.rdbuf(); do { is.get(*buf,'\n'); cout<<i<<". "<<buf<<"\n"; }while(is.eof()); 

    1 answer 1

     char buf[100000]; int main(){ ifstream is; is.open("log.txt"); for (int i=0; ;i++){ is.getline(buf,100000); if (is.fail()) break; cout << i <<". "<<buf<<endl; } 

    An example of working code.

    In your example, you use is.get(*buf,'\n'); those. istream& get (streambuf& sb, char delim); therefore, you must output what you received as a result of the function. Further is.eof() will always be true . we previously read is.rdbuf(); (to the end of the file). I did not find the variable i at all. The file name, by the way, is also necessary to take quotes.

    If you do not want to allocate static memory for buffer, then you can write this:

     int main(){ ifstream is; is.open("log.txt"); string buf; for (int i=0; ;i++){ getline(is,buf); if (is.fail()) break; cout << i <<". "<<buf<<endl; } 

    }