-1

I've looked up several instances in EOF, but in all instances EOF is being used on a file that is part of the program, for example:

std::fstream myFile("file.txt", std::ios::in);
while(!myFile.eof()) {
    /*program*/
}

However in this instance, I'm not using a file as part of the code. I'm just using basic cin commands. There's a command to quit, but let's say a user runs the program like this:

./program <myFile.txt> myOutput

Let's say that myFile had these commands in this:

add 1
add 2
delete 1
print

That's all fine, but they forgot to add a quit command at the end, so the code won't stop. So how do I get the code to detect EOF in this situation and stop?

Damerian
  • 49
  • 4
  • If they don't add a quit command but you reach the end of file that's probably a good time to stop. If they don't add a supported command, and the line you read has something unsupported that might also be a good time to stop (or you can continue reading to find another valid command, and eventually you'd end up at EOF anyway) – Tas Nov 06 '18 at 05:43
  • Your loop is broken anyway: https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – melpomene Nov 06 '18 at 05:47
  • `cin` is a "file" (input stream). – melpomene Nov 06 '18 at 05:58
  • If cin would work, then how would I go about comparing it to EOF? I don't think while(cin != EOF) would wor, would it? – Damerian Nov 06 '18 at 06:06

2 Answers2

0

End of file (EOF) means there is nothing more to read from the file buffer, it’s not something one puts explicitly at the file itself.. you should still get there fine with your code

Another way is to read the buffer until there are no more bytes to read there

RBreuer
  • 1,250
  • 1
  • 5
  • 13
  • _You should still get there fine with your code_ perhaps read https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Tas Nov 06 '18 at 05:47
0

The correct way to detect end-of-file is to check each input operation for success.

For example, with getline you'd use

std::string line;
while (std::getline(std::cin, line)) {
    // do stuff with line
}

Or with >>:

while (std::cin >> x) {
    // do stuff with x
}

This applies to all input streams, whether they're from files (fstream) or e.g. cin.

melpomene
  • 79,257
  • 6
  • 70
  • 127
  • I followed the one you did with cin and it seems to work., but I get segmentation fault. The output in myOutput is accurate, but why would the code give me a segmentation fault? – Damerian Nov 06 '18 at 06:13
  • @Damerian Because you made a mistake and your code accesses invalid memory? I can't be more specific because I can't debug code that I can't see. – melpomene Nov 06 '18 at 06:18
  • @Damerian Never assume you have only one mistake in your code. Often a tool such as [AddressSanitizer](https://github.com/google/sanitizers/wiki/AddressSanitizer) can help as can turning up the level of warnings issued by your compiler and resolving the resulting messages. Under g++ and compilers with similar command structure I use `-pedantic -Wall -Wextra -Werror` and under Visual Studio I generally specify /W4. If these provide no help, often your development environment will come with a debugging utility that allows you to closely inspect your program while it's running. – user4581301 Nov 06 '18 at 06:41
  • You're right, the segfault ended up being something completely unrelated, my apologies. – Damerian Nov 06 '18 at 09:09