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

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -