Trouble reading from file using getline() c++ -
this code prints last line of file. expected print text, 1 line @ time, file. idea why doesn't work?
string filename; cout << "file read: "; cin >> filename; ifstream afile; afile.open(filename.c_str()); string line; while(!afile.eof()) { getline(afile, line); cout << line; } afile.close();
trying way same thing:
for (string line; getline(afile, line);) { cout << line; }
maybe issue terminal? works...
for (string line; getline(afile, line);) { cout << line << endl; }
the problem last line printed. correct?
- i suggest add
std::endl
in while loop. can make issue more clear. output can confusing. - you can check line-delimiting character in input file.
'\n'
default delimitergetline
. if different character used, specifygetline
's 3rd parameter.
Comments
Post a Comment