1

So I'm having an issue where I am reading in a text file using cin. Here is a basic idea of my code:

while(getline(cin,line) {
cout << line << endl;
}
//Do some task
return 0;

The problem I'm running into is that the loop will not terminate and //Do some task will never run. The only solution that I've found is to look directly at the text file, see how many lines of text there are, and hard code a conditional to break out of it. So say I have a text file with 5 lines and an variable int row. Then I would do something like this:

  while(getline(cin,line) {
cout << line << endl;
if(row == 5) {
break;
}
//Do some task
return 0;

I tried googling but I can't seem to find an answer anywhere. Any ideas? And also only libraries I'm allowed to use is iostream.

2 Answers2

0

You can use rdbuf to redirect your cin output Following Link will help you. http://www.cplusplus.com/reference/ios/ios/rdbuf/

Hardik
  • 9
  • 2
0

The following code block should solve your issue: Credit goes to the author: kevinchkin http://www.cplusplus.com/forum/beginner/8388/

#include<iostream>
#include<fstream>

using namespace std;

int main() {

 ifstream myReadFile;
 myReadFile.open("text.txt");
 char output[100];
 if (myReadFile.is_open()) {
 while (!myReadFile.eof()) {


    myReadFile >> output;
    cout<<output;


   }
}
myReadFile.close();
return 0;
}

This is a basic template for reading from a .txt file. I also can not think of any reason why you should not be able to use more than just iostream. They make the other libraries because iostream isn't the best way to go about it. Most(if not all) Teachers/Professors I have had like it when students go above and beyond what the class is studying, and try to learn more. If you need additional help, take a look at: http://www.cplusplus.com/doc/tutorial/files/

  • 1
    Please see this thread: [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Ahmad Khan Sep 12 '17 at 05:45
  • I did say template, and I did link to where I got the code, hopefully RandomGuy looks there as well. What I saw in that thread is that you have to be careful how you use EOF, not that you shouldn't. My understanding of the code block here is that if we are not at the EOF, there is another line, so go get it. If we are at EOF, don't go grab what doesn't exist – Raum Schnee Sep 12 '17 at 05:49
  • `EOF` is just a flag, and the function `.eof()` just returns the value of the flag, so by making a call to `.eof()`, you can't tell always whether you're at the end of the file or not. What you can tell is, _whether the last statement I ran made the `EOF` flag true or not_. And the `EOF` flag only gets the value `true` if you try to read something from file and it finds the end of file character there, but you're reading from file in the body of the loop, so, by using the `eof()` in loop condition makes the loop run one extra time. – Ahmad Khan Sep 12 '17 at 05:59
  • That is a fair point, and I have learned something new here. however, would adding a try/catch to this help? you could listen for the error, and not use faulty data. I'm just not sure if that is bad coding practice to rely on an error. – Raum Schnee Sep 12 '17 at 06:02
  • also, would the EOF flag count as an input from the file, and not throw an error? – Raum Schnee Sep 12 '17 at 06:04
  • There can be many workarounds to this problem, one can be that you can check `eof()` again after reading from the file in the loop body (which I understand that you're referring to, maybe we can't use try/catch, but a call again to `eof()` would be fine). But there are better workarounds too. If you see the thread I provided you earlier, you'd find many things there. – Ahmad Khan Sep 12 '17 at 06:09