0
votes

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++?

1
Exactly what does your input look like, and can you provide more details of what goes wrong? [And I'm pretty sure index.push_back(index) isn't something you really do in your actual code]. - Mats Petersson
C++ operator>> to std:: string skips leading whitespace and terminates after non-whitespace content by default... just use while (inFile >> myString) .... - Tony Delroy
If you do end up using line by line input, may I recommend replacing std::basic_istream::getline with std::getline that reads to a std::string. This avoids worrying about the buffer yourself. - BoBTFish

1 Answers