Your loop is not correct. If you are actually getting the line, use getline(). Your code should look like one of the following:
1 2 3 4 5 6
inFile.open("/home/atg/a.txt");
vector<string> myStrs;
while (inFile >> Line) // This only reads one whitespace-delimited string from the file
{
myStrs.push_back(Line);
}
1 2 3 4 5 6
inFile.open("/home/atg/a.txt");
vector<string> myStrs;
while (getline(inFile,Line)) // This reads an entire line of the file
{
myStrs.push_back(Line);
}
The first version is good for reading a file like
Hello my name is John.
To get a list like:
"Hello", "my", "name", "is", "John."
The second is good for reading a file like
1 2 3
Hello.
My name is John.
Nice to meet you.
To get a list like:
"Hello.", "My name is John.", "Nice to meet you."
Hope this helps.
[edit] Consider the change. Your loop checks to see whether the file is still good. But inside the loop you attempt to read the file and then use the result without bothering to first check to see if the read succeeded.
Er, well, that example works, but it is still not a very good example because it copies EOF to cout.
Remember that you must always check for failure immediately after attempting to read anything from a file. Only if the read succeeded can you use the information you got, whether it be a single character or a whole line of them.