I am reading a text file which is written by a earlier routine. The text file contains the id and the path of certain data that i am processing. Because when i write to text file i used <<endl;
to go to the next line hence when i read the text file again it reads the white space. And the problem gets serious when i loop over again creating muliple whitespace and therefore i read and process garbage.
inFile.getline(buffer, 255);
string line(buffer);
istringstream iss_(line);
string index;
iss_ >> index;
index.push_back(index); // vector
my input text is generated after some process that contains list of text file directory and id. eg. D /Users/Student/Desktop/data1.txt S /Users/Student/Desktop/data2.txt
How can i avoid reading whitespace in C++?
index.push_back(index)
isn't something you really do in your actual code]. - Mats Peterssonoperator>>
tostd:: string
skips leading whitespace and terminates after non-whitespace content by default... just usewhile (inFile >> myString) ...
. - Tony Delroystd::basic_istream::getline
withstd::getline
that reads to astd::string
. This avoids worrying about the buffer yourself. - BoBTFish