LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 05-31-2003, 10:04 PM   #1
Bolt
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.

Regards

--
Bolt
 
Old 06-01-2003, 04:03 AM   #2
llama_meme
Member
 
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).

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

Rep: Reputation: 15
well...

... 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... ...you never know
 
Old 06-02-2003, 09:54 AM   #4
nautilus_1987
Member
 
Registered: Aug 2002
Distribution: Slackware 8.1
Posts: 750

Rep: Reputation: 30
Hi!
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
v2lk
Member
 
Registered: May 2003
Location: Estonia
Distribution: Slackware 9.1
Posts: 61

Rep: Reputation: 15
well...

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

while(some_condition){

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

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

if(input){
exit();
} else{
// do nothing, means, continue the "while" loop
} // end if
} // end while
 
Old 06-03-2003, 03:39 AM   #6
yrraja
Member
 
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.

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

Rep: Reputation: 111Reputation: 111
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".
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:06 AM.

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