-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;
1
  • 5
    Google "loop control structures in C++". Commented Mar 17, 2015 at 10:49

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Keep getting no such file or directory when it's there.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.