When using Getline () to read TXT files, do not pay attention to a little bit of a bug.
Post code:
#include <iostream>#include<string.h>#include<fstream>#include<sstream>using namespacestd;intMain () {Ifstreaminch("Aaa.txt"); strings; inti =0; while(Getline (inch, s)) {cout<<s; Const Char*p =S.c_str (); if(*p==' /') Break; I++; } cout<<Endl; cout<<i; return 0;}
The characteristic of Getline () is that it is the end of a line that encounters a ' \ n ', and the result is that the preceding data is stored as a string object.
Use the C_str () method to convert a string object to a C string of the const char* type, with a "." after its data.
Artificially changing the line after a TXT file causes the empty string object to be read multiple times, that is, I in the while is incremented several times.
So add a judgment, jump out in time. Multiple line breaks at the end of the file result in uncertainty over where the files end.
Multiple line break issues at the end of a read TXT file