-3

There is a text file I want to display, but I only get the first line, not sure how to do this:

string line;
ifstream myfile;
myfile.open("myfile.txt");
getline(myfile, line); 
cout << line << endl;
Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
user2786596
  • 135
  • 2
  • 5
  • 10

2 Answers2

16
    string line;
    ifstream myfile;
    myfile.open("myfile.txt");

   if(!myfile.is_open()) {
      perror("Error open");
      exit(EXIT_FAILURE);
   }
    while(getline(myfile, line)) {
     cout << line << endl;
    }

You just need to add a loop to get all lines of the file

alifirat
  • 2,899
  • 1
  • 17
  • 33
3

You are reading line just once with one call of getline(myfile, line); You need to do that in a loop until all lines are read. Same question

Community
  • 1
  • 1
riodoro1
  • 1,246
  • 7
  • 14