I am using ifstream
to get lines from a file and store them to a string. Each line contains a single word with no spaces.
virtual void readFromFile(char* input){
ifstream inf(input);
if(!inf){
cerr << "The specified file could not be found." << endl;
}
while(inf){
string str;
getline(inf, str);
pushFront(str); // store it in my data structure
}
inf.close();
}
file.txt
a <= returns length 1 (correct)
at <= returns length 3
ate <= returns length 4
rate <= returns length 5
irate <= returns length 6
When I call length()
on the string corresponding to the very first of the file, it returns the correct value. However, calling length
on strings corresponding all other lines results in a offset of +1. For example, if the length of the string is actually 5, it returns 6. Does this have something to do with new lines? If so, how can I properly extract these words from the file?