For the first while statement, it only applicable for file redirection eg "programname < filename". If i use the first while statement for getting a standard input without the file redirection, the user have to press the control D button in order to exit the while statement.

As for the second while statement, the user able to enter the standard input manually and then press enter and it will exit the while loop. But for the file redirection, it is only able to get the first line in the file and not the whole file.

How can i implement a while statement where both the condition will be able to work which means when the user use the file redicrection, it is able to read till the end of the file and also when the user enter the input manually from the standard input and press enter, it will exit from the loop ??

Code:

// used for file input redirection
while((c = getchar()) != EOF)
{

}
// used for command line input
while((c = getchar()) != '\n')
{

}

04-04-2004

quzah

In short, you can't. Redirection is identical to you typing on the keyboard. There is no difference as far as C is concerned. So, you either have it check for EOF, and the user must then enter EOF themselves, or you have it read one line at a time.

The closest to getting what you want is going to be setting up some form of prompt:

But again, there's always the slight chance that the first byte of the piped file matches something, so it throws off your efforts. So in closing, you can try, but there's no guarintee. But really, is it all that much trouble to prompt for CTRL+D from your users?