Thread: How to check if stdin is empty before clearing anything left in stdin

  1. #1
    Registered User
    Join Date
    Jul 2019
    Posts
    3

    How to check if stdin is empty before clearing anything left in stdin

    I have been using the code...
    while (getchar() != '\n');
    ...to clear everything in stdin in my programs. However, I am running into a slight issue on some occasions. There are times in my programs where when I call getchar(), there may or may not be anything in stdin. When it is the case that there is nothing in stdin, what ends up happening is getchar() is called and it is waiting for something in stdin. I then have to enter \n on my keyboard otherwise the program is just stuck on that one line. I am wondering if there is a simple way to first check if stdin is empty before doing while (getchar() != '\n'); in order to avoid this problem?

    Not sure if the compiler matters for the answer, but I am using Visual Studio 2017.

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    I don't have the slightest idea on how to do this on Windows (and I don't care!). To check if the standard input has any data available, in Linux, is simple:
    Code:
    struct pollfd pfd = { .fd = fileno( stdin ), .events = POLLIN };
    if ( poll( &pfd, 1, 0 ) > 0 )
      if ( pfd.revents & POLLIN ) { ... there is data in stdin ... }
    On other Unix systems you can use select()...

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    There is no portable way for you to do this. The thing most programmers do is to always read lines with fgets(), and then parse the lines with sscanf().

    Now, this may appear stupid, as many and all resources and tutorials tell you to avoid this, but in Windows, as per the Microsoft Docs, fflush() can be used to clear the input stream.
    Devoted my life to programming...

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Quote Originally Posted by GReaper View Post
    There is no portable way for you to do this. The thing most programmers do is to always read lines with fgets(), and then parse the lines with sscanf().

    Now, this may appear stupid, as many and all resources and tutorials tell you to avoid this, but in Windows, as per the Microsoft Docs, fflush() can be used to clear the input stream.
    If in fact, Widoze does allow "fflush(stdin)", DON"T DO IT!

    It will make your code non-portable, as with using ANY Non-Standard MS extension to the C Programming Language!!!

    Please stick to the C11 C Standard!

  5. #5
    Registered User
    Join Date
    Jul 2019
    Posts
    2
    This post is so good to see very helpful for me.

  6. #6
    Registered User
    Join Date
    Jul 2019
    Posts
    3
    Ok, thanks for the response. Unfortunately I'm only 1 semester in to my CS degree, so none of that code makes sense to me, nor do I know what select is()...Do you know of any simpler ways? If not that's okay, I will come back to your code in the future...my schoool does use Linux at some point.

  7. #7
    Registered User
    Join Date
    Jul 2019
    Posts
    3
    Quote Originally Posted by flp1969 View Post
    I don't have the slightest idea on how to do this on Windows (and I don't care!). To check if the standard input has any data available, in Linux, is simple:
    Code:
    struct pollfd pfd = { .fd = fileno( stdin ), .events = POLLIN };
    if ( poll( &pfd, 1, 0 ) > 0 )
      if ( pfd.revents & POLLIN ) { ... there is data in stdin ... }
    On other Unix systems you can use select()...
    Ok, thanks for the response. Unfortunately I'm only 1 semester in to my CS degree, so none of that code makes sense to me, nor do I know what select is()...Do you know of any simpler ways? If not that's okay, I will come back to your code in the future...my schoool does use Linux at some point.

  8. #8
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by mweston379 View Post
    Ok, thanks for the response. Unfortunately I'm only 1 semester in to my CS degree, so none of that code makes sense to me, nor do I know what select is()...Do you know of any simpler ways? If not that's okay, I will come back to your code in the future...my schoool does use Linux at some point.
    If you use Debian (or Ubuntu), install manpages-dev package:
    Code:
    $ sudo apt install manpages-dev
    And, afterwards:
    Code:
    $ man poll
    or
    Code:
    $ man select
    With manpages-dev package you can get almost all functions manuals (at volume 3 - and there are lots of useful info on volume 7 of manuals... you specify the volume by number, as in 'man 3 printf' -- since 'man printf' will get you the command printf [from shell]).

    There are lots of useful info on a manual page: Pay attention to DESCRIPTION, RETURN VALUE, ERRORS and SEE ALSO topics -- sometimes there is an EXAMPLES topic (poll has one!).

    Here's an example of 'man poll':
    How to check if stdin is empty before clearing anything left in stdin-untitled-jpg
    Last edited by flp1969; 07-05-2019 at 02:35 PM.

  9. #9
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    @flp1969

    Actually, if the man pages for the Standard Library are not installed during the basic installation of a Debian or Ubuntu Distro, the package, "build-essential", should install all that is needed for C development, including the man pages.

  10. #10
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by rstanley View Post
    @flp1969

    Actually, if the man pages for the Standard Library are not installed during the basic installation of a Debian or Ubuntu Distro, the package, "build-essential", should install all that is needed for C development, including the man pages.
    Maybe, but I don't think so:

    Code:
    $ apt depends build-essential
    build-essential
     |Depends: libc6-dev
      Depends: <libc-dev>
        libc6-dev
      Depends: gcc (>= 4:7.2)
      Depends: g++ (>= 4:7.2)
      Depends: make
        make-guile
      Depends: dpkg-dev (>= 1.17.11)
    No manpages-dev there! Even if you chek for the dependencies for libc6-dev...

  11. #11
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    If you are using Windows you can use conio.h - > kbhit()

    On Windows it is really easy to see if something is on stdin before trying to read

    Code:
    #include <conio.h>
    ... 
    if (kbhit()) 
    {
      // something on stdin
    }
    Last edited by Click_here; 07-06-2019 at 03:46 AM.

  12. #12
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Oh I see - You are not checking for EOF...

    Code:
    #include <stdio.h> 
    
    int main(void) 
    {
     int ch;
     char buf[BUFSIZ];
     puts("Flushing input");
     while ((ch = getchar()) != '\n' && ch != EOF);
    ...
    From here: Flush the input buffer - Cprogramming.com

  13. #13
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Quote Originally Posted by flp1969 View Post
    Maybe, but I don't think so:

    Code:
    $ apt depends build-essential
    build-essential
     |Depends: libc6-dev
      Depends: <libc-dev>
        libc6-dev
      Depends: gcc (>= 4:7.2)
      Depends: g++ (>= 4:7.2)
      Depends: make
        make-guile
      Depends: dpkg-dev (>= 1.17.11)
    /usr/share/doc/build-essential/list'

    No manpages-dev there! Even if you chek for the dependencies for libc6-dev...
    "build-essential" contains a list of "dependencies" that trigger the installation of all that is needed for a basic C & C++ development environment. One of those "dependencies" will trigger the installation of the man-pages.

    I have done MANY Debian installations, and I have NEVER needed to manually install the man-pages needed. If a package installed has associated man-pages, it will list the man-pages package as a dependency!

    Do a basic installation of Debian or Ubuntu, and then install one package, "build-essential", and watch all the packages that are installed, as dependencies of "build-essential" and subsequent packages.

  14. #14
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Quote Originally Posted by Click_here View Post
    If you are using Windows you can use conio.h - > kbhit()

    On Windows it is really easy to see if something is on stdin before trying to read

    Code:
    #include <conio.h>
    ... 
    if (kbhit()) 
    {
      // something on stdin
    }
    Once again, I don't recommend the use of conio.h or ANY MS extension to the C Programming Language!

    You are locking yourself into a Windows compiler environment. The code CANNOT be ported to Linux or any other non-windows compiler, without replacing the MS specific functions.

    Please stick to Standard C, and please advise others to do so as well!!!

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    As per rstanley's firm insistence, please stick to standard C, and hence ignore flp1969's post #2. Both poll and select are non-standard with respect to C11.

    Besides Click_here's mention about EOF that doesn't help differentiate what you're looking to do, the only standard answer you have received thus far is this one:
    Quote Originally Posted by GReaper
    The thing most programmers do is to always read lines with fgets(), and then parse the lines with sscanf().
    Another answer is that you can simply use thst loop only after a read that leaves a newline in the input buffer.

    Note that fgets won't necessarily read the entire line as it could be too long, so you might need to use it in a loop until a '\n' is stored by fgets. Some people might suggest the use of POSIX getline instead, but alas, that is non-standard.
    Last edited by laserlight; 07-06-2019 at 10:35 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Clearing stdin
    By qwertyuiop23 in forum C Programming
    Replies: 6
    Last Post: 08-14-2011, 11:54 PM
  2. Blocking or clearing stdin
    By woopitt in forum C Programming
    Replies: 4
    Last Post: 05-23-2011, 05:05 PM
  3. How to check if stdin needs to be flushed?
    By DKarhi in forum C Programming
    Replies: 4
    Last Post: 09-20-2007, 04:51 PM
  4. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  5. ascii 10 in the stdin after fflush(stdin)??
    By Kev2 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 06-03-2002, 03:53 PM

Tags for this Thread