-
stdin
is there a way to check stdin for more data without asking for more? a file when read in through the "<" symbol appears in stdin. or i can read answers from a person instead of a file. i use this command for file input:
Code:
fscanf(stdin,"%[^\0]",input);
and this command for regular input:
Code:
fscanf(stdin,"%[^\n]",input); // or something equivalent using fgets
i need a way to test the stdin to see if it's done without asking for more data. any ideas? :)
-
Not without knowing which OS you're using
-
let's say.... dos, although i want the code to be portable if possible.
-
Tricky
You can check whether there is any user input using the kbhit() function, but as you know, stdin isn't necessarily the keyboard.
Ordinarily, stdin is buffered, so the program only gets a whole line's worth of data when you press enter.
You might try turning off buffering by using setbuf or setvbuf, but it didn't work here.
Using conio.h functions directly (non-portable)
Code:
#include <stdio.h>
#include <conio.h>
int main ( ) {
int ch;
while ( 1 ) {
if ( kbhit() ) {
printf( "Key hit " );
ch = getch();
printf( "Val=%d\n", ch );
if ( ch == '\n' ) break;
}
}
return 0;
}
This would allow you to build a string one char at a time, which you could then use sscanf on (say) once you had read the newline.