How to Read Stdin Without Waiting for Enter C

INTELLIGENT Work FORUMS
FOR Estimator PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you lot a
Figurer / IT professional person?
Join Tek-Tips Forums!

  • Talk With Other Members
  • Exist Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Admission To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It'due south Costless!

*Tek-Tips's functionality depends on members receiving east-postal service. By joining you lot are opting in to receive electronic mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How exercise check the stdin without stopping?

2

  • Forum
  • Search
  • FAQs
  • Links
  • MVPs

How do check the stdin without stopping?

How do bank check the stdin without stopping?

(OP)

ok, this is my problem. I'm running an space loop where i am gathering data and writing information technology to a file.  I desire to exist able to cheque the stdin then the user tin input 'q' or something  like that so suspension the loop.  The problem is that scanf(), getchar(), getc(), etc end the loop and look for the user to press "Enter".  is in that location anyhow of checking the stdin without stopping the plan? Please Help.

RE: How do check the stdin without stopping?

It depends on what OS you lot are using.

1) DOS/Windows - use kbhit or _kbhit.  Information technology lives in conio.h and volition tell you whether the user has hitting the keyboard or not.  If the user has hit the keyboard then you can exercise a getch otherwise continue looping.

2) Unix - use pthreads: create ii threads: 1 to poll the keyboard and one to do your continuous loop

3) Any other OS - I haven't got a inkling.

RE: How exercise check the stdin without stopping?

Example of using pthreads with a polling loop.  This is written with a C++ compiler and then in that location may exist $.25 that practice not compile with C.

#include <pthread.h>
#include <stdio.h>

struct Mutual
{
    bool mRunning;    /* Tells u.s.a. when to quit */
    float mCount;
};
/* The number crunching bit */
void* Cruncher (void* info)
{
    struct Common* cptr = (struct Common*) info;
    cptr->mCount = 0.0;
    while (cptr->mRunning)
    {
        cptr->mCount++;
    }

    return (void*) 0;
}

void* PollKbd (void* info)
{
    struct Mutual* cptr = (struct Common*) info;
    int ch;

        printf ("Printing q followed by return to quit\due north");
    practise {
        ch = getchar ();

    } while (ch != 'q');
    cptr->mRunning = false;

    render (void*) 0;
}

principal ()
{
    pthread_t crunchThread, pollThread;
    struct Mutual common = { true, 0.0 };

    pthread_create (&pollThread, 0, PollKbd, &mutual);
    pthread_create (&crunchThread, 0, Cruncher, &common);

    pthread_join (pollThread, 0);
    pthread_join (crunchThread, 0);

    printf ("Count got up to %f\n", mutual.mCount);

    render 0;
}

RE: How do check the stdin without stopping?

fork the job
child writes to files
father reads stdin, cease child if got 'q'
or what always you lot want

voice clamantis in deserto.

RE: How do cheque the stdin without stopping?

The posix thread solution seems okay.
A quicker, easier solution might be to check the input
queue to see if anything'south available.
   This curt example just increments a counter until
the user presses a 'q' and <enter>.
   Usually, the input will be in "cooked" mode, and so
whatever is typed will not exist available for reading
until the <enter> is pressed.  And then suddenly everything
is fabricated bachelor.  If the input is in "raw" (or "cbreak")
fashion, so each character is fabricated available as it's typed.
Utilise the stty command to endeavour that before running it.

This is a posix thing, for Linux and the like.  This detail
instance was tested on a Solaris eight car.

Hope that helps!
-BUD


#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/filio.h>

main( int c, char *five[])
{
    int numAvailable;
    char buf[100];
    int counter;

    for( counter = 0; ; ++counter) {

        sleep( 1);
        fprintf( stdout, "%d\due north", counter);

        /*
         * encounter if anything is bachelor on the input queue
         */
        if( ioctl( 0, FIONREAD, &numAvailable) < 0) {
            fprintf( stderr, "ioctl failed: %south\n", strerror( errno));
            break;
        }

        /*
         * input awaits - a read won't block now
         */
        if( numAvailable <= 0)
            continue;

        /*
         * one wouldn't typically use read.
         */
        if( read( 0, buf, sizeof buf) <= 0) {
            fprintf( stderr, "read failed: %s\due north", strerror( errno));
            break;
        }
        /*
         * as long equally the standard input is in normal "cooked"
         * mode, the read call will never return 1; at that place will
         * always be at least one more byte bachelor, the '\north'.
         * Null is bachelor for reading until the user
         * presses return.
         */
        if( buf[0] == 'q') {
            fprintf( stderr, "Quit.\n");
            break;
        }
    }
}

RE: How exercise cheque the stdin without stopping?

I always wondered what the Unix equivalent of kbhit was.  Cheers BigUglyDude.

Scarlet Flag Submitted

Thank you for helping keep Tek-Tips Forums gratuitous from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Bring together | Advertise

Copyright © 1998-2022 engineering.com, Inc. All rights reserved.
Unauthorized reproduction or linking forbidden without expressed written permission. Registration on or utilise of this site constitutes acceptance of our Privacy Policy.

Engineering.com

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical estimator professional person customs.
It's easy to join and it's free.

Here'due south Why Members Dear Tek-Tips Forums:

  • Tek-Tips ForumsTalk To Other Members
  • Notification Of Responses To Questions
  • Favorite Forums 1 Click Access
  • Keyword Search Of All Posts, And More than...

Register now while information technology's still free!

Already a member? Close this window and log in.

Join Us Close

ezelltorgent.blogspot.com

Source: https://www.tek-tips.com/viewthread.cfm?qid=371103

0 Response to "How to Read Stdin Without Waiting for Enter C"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel