Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-31-2003, 10:04 PM   #1
LQ Newbie
Registered: May 2003
Posts: 7

Rep: Reputation: 0
Simple C Programming Question..

Hi all,

I am a university student doing a project on uClinux Coldfire boards. I am faced with a daunting yet simple problem which I can't get my head around.

Is there any way for me to check for user input when inside a while loop? If the user hasn't inputed anything then the while loop just continues. Basically I want the user to be able to terminate the while loop.

At the moment scanf and other similar functions that I know wait for user input. There is no timeout.

Does anyone know of a better alternative than this. I can't really fork out a new process because of uClinux only supports vfork.


Old 06-01-2003, 04:03 AM   #2
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
You could try using non-blocking I/O (though the C standard library doesn't support this, of course).

Old 06-02-2003, 07:32 AM   #3
Registered: May 2003
Location: Estonia
Distribution: Slackware 9.1
Posts: 61

Rep: Reputation: 15

... i've never used c in practice, so i may be wrong, but maybe you could test stdin against true or false and so decide if there was any input. on second taught, there really must be an option to read (a byte) directly from stdin, and it should be zero if no input.
but... never know
Old 06-02-2003, 09:54 AM   #4
Registered: Aug 2002
Distribution: Slackware 8.1
Posts: 750

Rep: Reputation: 30
Can you please post a fragment of code where you have problems (if it is not so conspirative)?
Old 06-02-2003, 11:42 AM   #5
Registered: May 2003
Location: Estonia
Distribution: Slackware 9.1
Posts: 61

Rep: Reputation: 15

...i maybe answered wrong, but i think i understood what he ment:


printf("Hello world!\n");
printf("Hit any key to stop printing \"Hello World!\"!\n");

input= // how to determine that a key was pressed?

} else{
// do nothing, means, continue the "while" loop
} // end if
} // end while
Old 06-03-2003, 03:39 AM   #6
Registered: Sep 2002
Distribution: RH, FC, Ubuntu, Solaris, AIX
Posts: 114

Rep: Reputation: 15
Use select() for this. Add STDIN in the read list of select and set the timer value to 0. This way when the the application will come to select(), it will simply check if the there in some input. If there is not input if will immidiately go forward.

Old 06-03-2003, 12:05 PM   #7
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 110Reputation: 110
Yes, select() is what you're looking for.
It is a bit of a tricky function to use, though.
So, apart from "man select" itself, you may want to read "man select_tut".


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
simple shall programming question itz2000 Programming 1 11-05-2005 04:12 PM
very simple bash programming question!? Thinking Programming 7 06-01-2005 11:07 AM
Simple C programming Spooky Programming 5 10-22-2004 03:16 AM
simple question on shell programming stupid_guy Programming 5 03-10-2004 02:07 PM
A simple perl programming question J_Szucs Programming 12 07-14-2002 10:03 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:24 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration