LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 02-17-2009, 09:01 PM   #1
charlitos
Member
 
Registered: Feb 2009
Posts: 51

Rep: Reputation: 16
C - Unix check for ^D


I want to take input from the stdin into my c program but I want to terminate it when the user presses (Control - D), how can I check this from my program? How can I know the user just entered ^D?
 
Old 02-17-2009, 10:37 PM   #2
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
How can I know the user just entered ^D?
If you're reading from stdin, I hope you're using function fgets(); see its man page.

That man page will show you that fgets() returns NULL on end of file, and the ^D which triggered the end of file will be absorbed by the function itself; you'll never see it. Just test for that NULL.
 
Old 02-18-2009, 12:35 AM   #3
charlitos
Member
 
Registered: Feb 2009
Posts: 51

Original Poster
Rep: Reputation: 16
I was thinking of two things, one to store all the input in a single variable other one to store the input in a file. I want to do it the second way but I dont know exactly how to append line by line to this file, also I should have a way to create this new file from scratch cause I guess otherwise itll be full of garbage every time I run my script.
 
Old 02-18-2009, 09:46 AM   #4
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
I dont know exactly how to append line by line to this file, also I should have a way to create this new file from scratch cause I guess otherwise itll be full of garbage every time I run my script.
These two things, appending line by line to a file and recreating a file from scratch (removing any old content) are performed by library functions fopen() and fprintf().

See their man pages for further info.
 
Old 02-18-2009, 01:02 PM   #5
charlitos
Member
 
Registered: Feb 2009
Posts: 51

Original Poster
Rep: Reputation: 16
Thanks a lot guys, I had it working
 
  


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
LXer: Using Kstat To Check Your NIC Settings On Solaris 10 Unix LXer Syndicated Linux News 0 02-10-2009 03:40 PM
LXer: Speaking UNIX, Part 4: Setting and managing permissions on UNIX LXer Syndicated Linux News 0 10-21-2006 01:54 AM
script to check instance of PHP & mySQL running on Unix platform kushalkoolwal Programming 7 04-26-2006 09:40 PM
Add a notice of the top 20 commands to check your unix system on the front page t3gah LQ Suggestions & Feedback 1 03-21-2005 08:23 AM
Boot disk; check. CD in drive; check. Doesn't work; check. Hal DamnSmallLinux 7 02-04-2004 02:10 AM

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

All times are GMT -5. The time now is 11:08 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