stdin

This is a discussion on stdin within the C Programming forums, part of the General Programming Boards category; is there a way to check stdin for more data without asking for more? a file when read in through ...

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493

    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?
    Last edited by ygfperson; 02-09-2002 at 08:47 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,828
    Not without knowing which OS you're using

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    let's say.... dos, although i want the code to be portable if possible.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,828
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking stdin for input
    By hawaiian robots in forum C Programming
    Replies: 7
    Last Post: 05-19-2009, 10:06 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  3. Replies: 2
    Last Post: 07-12-2008, 01:00 PM
  4. value of stdin (not zero?)
    By taisao in forum Linux Programming
    Replies: 3
    Last Post: 05-27-2007, 09:14 AM
  5. stdin question
    By oomen3 in forum C Programming
    Replies: 6
    Last Post: 02-19-2003, 02:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21