-2

So after I have read in a text file for my C++ program, when I go to read in the last line of my file using getline it goes into an infinite loop. Now I know this has been asked before on stack overflow, but my problem is I am not supposed to use .eof, or anything related to the ifstream library, which all of the answers I have checked contain.

How can I signal my program that the end of the file has been reached WITHOUT using .eof or anything in < ifstream > so that I can prevent this infinite loop?

ildjarn
  • 59,718
  • 8
  • 115
  • 201
user5482356
  • 463
  • 1
  • 6
  • 21
  • 1
    Paste the code please. – Jagannath Jan 20 '16 at 05:22
  • @Jagannath My program is for a school project and I risk facing disciplinary action if my code is found online, so I would prefer not to. Is it possible to diagnose the issue without doing so? – user5482356 Jan 20 '16 at 05:26
  • also my code is all over the place and spans 500 lines so it might not be the best option. – user5482356 Jan 20 '16 at 05:27
  • Which answers contain eof? – n. 'pronouns' m. Jan 20 '16 at 05:29
  • Provide an [MCVE](http://stackoverflow.com/help/mcve). – n. 'pronouns' m. Jan 20 '16 at 05:31
  • That you're not supposed to use `eof` is not a restriction, since [you shouldn't use `eof`](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) anyway (so your teacher is helping you avoid a bug). That you're not supposed to use anything related to `ifstream` must be a misunderstanding, as that means that you can't read your file at all. – molbdnilo Jan 20 '16 at 05:37
  • My crystal ball thinks that your loop condition involves the last value read by `getline`, not the value `getline` returns. – molbdnilo Jan 20 '16 at 05:46
  • @molbdnilo I am using input/output redirection through the command line and just use standard input/output within the program itself. To test, I link to text files in my directory using getenv (something to do with environments in Xcode, which I use -- I don't really understand it) – user5482356 Jan 20 '16 at 06:02
  • @n.m. while(line_is_comment(line)) { getline(cin, line) }, //where "line" is the string storing the line and "line_is_comment" is a function that returns true if the first character of the line == ' / ' – user5482356 Jan 20 '16 at 06:06

1 Answers1

2

You just need to check the stream in a boolean context which will determine whether the stream is in a failure state. And since getline returns the stream itself, you can just check the result of that.

while (std::getline(FileStream, Line)) {
    // process line
}
Benjamin Lindley
  • 95,516
  • 8
  • 172
  • 256
  • the problem is I am not using a filestream to begin with. I am using input/output redirection via the command line (I use Xcode) and using a getenv (environment) function to read in test files within my directory – user5482356 Jan 20 '16 at 06:03
  • @user5482356: Then replace `FileStream` with `std::cin`. – Benjamin Lindley Jan 20 '16 at 06:04