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?

  1. i suggest add std::endl in while loop. can make issue more clear. output can confusing.
  2. you can check line-delimiting character in input file. '\n' default delimiter getline. if different character used, specify getline's 3rd parameter.

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -