0
#define SIZE 30

 // some code
ifstream outFile;   
outFile.open("lab.txt");   // opening lab document which has a sentence
char buffer[SIZE];         // buffer for storing the sentence when reading from file

while (!outFile.eof())     // reads lab.txt until eof
{   
    // two methods for output to screen which print sentence on debugging
    // but program doesn't terminate 
    outFile >> buffer;
    cout << buffer << endl;

    // outFile.getline(buffer, SIZE);
    // cout << buffer << endl;
}

this code is for reading from a sequence file, for example lab.txt. The problem is that, when i run the sentence in the file, i gets printed but is followed by an infinite loop which keeps on printing spaces, so the program never terminates. I've tried both ways but both result is the same. Any help would be welcomed?

F. T.
  • 27
  • 7
  • Don't use `.eof()` – Rakete1111 May 16 '16 at 16:22
  • @Rakete1111 now it doesnt print anything. – F. T. May 16 '16 at 16:31
  • What did you use then? You should loop over the return value of `operator< – Rakete1111 May 16 '16 at 16:32
  • 1
    Just a thing to bring up as it confused the heck out of me. You are reading a file but you name the stream `outFile`. The name of the variable is totaly inconsistent with it's use. I would suggest something like `fin`(**f**ile **in**) or `infile` for input file streams. – NathanOliver May 16 '16 at 16:37
  • Yes, it is rather confusing. But our lecture had it his way. Thank you for suggesting. it will definitely improve my code. @NathanOliver – F. T. May 16 '16 at 17:04

2 Answers2

2

The correct way of reading from file is as follows:

while(outFile >> buffer) {
    cout << buffer << endl;
}

Using outFile.eof() as condition is problematic.

Eissa N.
  • 1,495
  • 9
  • 16
  • Thank you this worked..:) – F. T. May 16 '16 at 16:33
  • @F.T. Then upvote and accept. – Matsmath May 16 '16 at 16:34
  • 1
    @F.T. Note that this does not alone let you discern between errors and reaching a successful end of file. To have a complete understanding of the issues, I suggest reading something like ["dealing correctly with badbit, failbit, eofbit, and perror()"](https://gehrcke.de/2011/06/reading-files-in-c-using-ifstream-dealing-correctly-with-badbit-failbit-eofbit-and-perror/). – HostileFork says dont trust SE May 16 '16 at 16:39
  • Note: `outFile >> buffer` will gleefully overflow `buffer` if the stream contains a sequence that runs for 30 or more characters without whitespace. This applies to the initial question code as well. Consider using `std::string` rather than a `char` array for `buffer`. – user4581301 May 16 '16 at 16:39
  • @user4581301 I agree with you and I would suggest using `std::string buffer;` instead of `char buffer[SIZE];` – Eissa N. May 16 '16 at 16:42
  • the char array was the demand of the task although doing it in string is easy. thank you for your help though. Really appreciate it – F. T. May 16 '16 at 16:50
1

just use

while(outFile.getline(buffer, SIZE))
{
    .....
}

instead of outFile.eof()

kuro
  • 2,585
  • 1
  • 12
  • 23
The Philomath
  • 951
  • 9
  • 14
  • Right idea, but why `getline`? – user4581301 May 16 '16 at 16:37
  • firstly overloaded operator >> will not accept the leading spaces so it is better to use getline. second getline handles the buffer overflow problem. It would be much better if we use std::string line; here and check while(getline(outfile,line)) – The Philomath May 16 '16 at 16:53
  • 1
    The buffer overflow point is well taken, but this shifts the input to line-based from token based. When you change how the OP's code works, you should explain in the answer why you made the change and how your solution is better. – user4581301 May 16 '16 at 17:41