tags:

views:

159

answers:

5

Does stdin has any EOF? For e.g - If i start reading from stdin using fread/read then when the following loop will end?

while ((c = read(0, buffer, BUFSIZ)) > 0) {
    .
    .
    .
}

If the solution of above is no the is there any way of added EOF in the stdin?

+1  A: 

I have never programmed C in windows so I can't tell you but in bash, the program will get an EOF when you type end of data (Ctrl+D)

sui
In MS-DOS the Ctrl+Z key will also signal an EOF condition. (But not on *nix systems).
Thomas Matthews
Yes, basically this is sending EOF to the stream
LukeN
@LukeN Actually it just closes the stream. There is no actual EOF character.
Bart van Heukelom
@Bart van Heukelom so after typing Ctrl+D, what should i do if i want to read more inputs from stdin later on? is it possible?
Ravi Gupta
If you're using stdio, you can call `clearerr(stdin);` to clear the EOF status and you will be able to continue reading. (If stdin is a file that hasn't grown since you got EOF, you'll just get a new EOF condition as soon as you try to read.) If you're using low-level `read()`, it returns 0 on when the terminal has EOF status, but if you try to read again it will block waiting for input.
R..
+3  A: 

Speaking about EOF in stdin: when you redirect input from file, e.g.:

program <input.txt

the file already has an EOF, so this is not a problem. In console you can simulate EOF flag. In UNIX systems it is Ctrl+D, in Windows Ctrl+Z. When you type this in the console, program will behave like it has just reached end of input file.


Edit

According to a question asked by OP:

So does it means that stdin don't have EOF and we have to insert them manually using Ctrl+Z or Ctrl+D?

Actually -- yes. One may consider stdin (not redirected, but taken from the console) as infinite file -- no one can tell where does it end. The end of input file, where input ist stdin, must be told literally by Ctrl+D or Ctrl+Z.

Archie
A: 

The way to test for EOF is to check the return value of fread, and then use feof:

while( fread( ... ) ) {    // loop so long as fread does not return zero
    // do something
}

if ( feof( stdin ) ) {
   // read failed because of EOF
}
else {
   // some other error
}
anon
and what if `while( fread( ... ) )` loop never returns?
Ravi Gupta
@Ravi That should not happen.
anon
why? ... you mean to say the `while` loop will return after a finite number of iterations ... if so the after how many?
Ravi Gupta
@Ravi fread() attempts t read data - if there is nothing to read, it returns zero. So the loop above will read standard input or a file until an EOF is encountered.
anon
@Neil Butterworth ok ... i tried your code and it gives `some other errors`. So does it means that stdin don't have EOF and we have to insert them manually using Ctrl+Z or Ctrl+D?
Ravi Gupta
It could mean that stdin contained a 0
Bart van Heukelom
@Bart No, it couldn't.
anon
@Ravi The code I posted is not intended to be compilable.
anon
@Neil Butterworth actually i filled the other details to make it a complete program. Here is the entire code. `#include <stdio.h>#include <stdlib.h>#include <string.h>#define SIZE 65536int main(){ char buffer[SIZE]; int c; while ((c = fread(buffer, 1, SIZE, stdin)) > 0) { printf("Inside while\n"); } if ( feof( stdin ) ) { printf("read failed because of EOF\n"); } else { printf("some other error\n");} return 0;}`
Ravi Gupta
@Neil: Indeed. I wrongly assumed fread() returns 1 read byte (as read() on Java streams does).
Bart van Heukelom
A: 

Your solution is tagged C++, so here's some C++.

std::string lols;
while(!(std::cin >> lols).eof()) { // This loop will end when EOF is reached
    // Process string
}
DeadMG
will the loop ends? .... that what i am asking ... i am not asking different ways of writing the same code.
Ravi Gupta
You have overcomplicated that and in doing so broken it. If the read fails for some other error then the stream will be marked bad and not read anymore but the EOF will not be reached and thus loop forever. The simpler and correct way to loop would be: 'while( std::cin >> lols) {}'
Martin York
@Martin York: I did originally have it without the EOF, but the OP's comment shows that he could really use the hint about what's going on. @Ravi Gupta: It will keep going until EOF. That's why I put in the EOF function check. So that it's goddamn obvious that the code executes until EOF. And there's a comment there, telling you that the loop will end when EOF is reached. I mean, this code is pretty self explanatory.
DeadMG
A: 

while ((c = read(0, buffer, BUFSIZ)) > 0) {

You don't say the type of c but using that name implies that it's a char. Note that the EOF value for iosteams is an (int) -1. Storing that into an unsigned char will get you a value of 255 which will not match EOF.

James Curran
ok ... `c` is of `int` type ... i was just giving the example .... now come back to the original problem and according to you, what is the ans?
Ravi Gupta