C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-20-2010, 06:53 PM   #1
Registered User
 
Join Date: Mar 2010
Posts: 7
Reading Ctrl D in fgets with some inputs

Hi to all,

I was wondering if you guys can help me with this tricky stdin problem?

Suppose I want my c program to accept input. But at the same time I want to detect Ctrl D as well.

For example,

Your input: test123^D

^D is shown when I press the combination keys Ctrl D.

How do I detect Ctrl D if there are characters before it? For the record, I am able to detect Ctrl D by itself.

Your input: ^D<Press Enter>

Thanks.
CashCow01 is offline   Reply With Quote
Old 03-20-2010, 07:58 PM   #2
Registered User
 
claudiu's Avatar
 
Join Date: Feb 2010
Location: London, United Kingdom
Posts: 1,062
As far as I know CTRL+D is actually EOF. So test for EOF.
claudiu is offline   Reply With Quote
Old 03-20-2010, 10:09 PM   #3
Registered User
 
claudiu's Avatar
 
Join Date: Feb 2010
Location: London, United Kingdom
Posts: 1,062
It's always 4.

check out this useful link:

Ascii control codes (control characters, C0..controls)
claudiu is offline   Reply With Quote
Old 03-20-2010, 10:17 PM   #4
Registered User
 
Join Date: Mar 2010
Posts: 14
K always 4 then, how about a solution??
magestrium is offline   Reply With Quote
Old 03-20-2010, 10:20 PM   #5
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,556
you haven't asked and OP haven't said if it is Windows or Unix

you do not check return value of scanf...
you do not guard against buffer overrun in scanf
and void main - read FAQ why you should not use it
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 03-20-2010, 10:33 PM   #6
Registered User
 
claudiu's Avatar
 
Join Date: Feb 2010
Location: London, United Kingdom
Posts: 1,062
Magestrium it is against forum rules to provide someone with the entire solution tp his problem before he even got to start working on it. So please don't lecture me. Besides, your solution is poor mainly due to reasons pointed above by vart. It is also against forum rules to post chunks of code REPEATEDLY without using code tags.

Nobody will read them mate.
claudiu is offline   Reply With Quote
Old 03-21-2010, 12:21 AM   #7
Registered User
 
Join Date: Mar 2010
Posts: 7
Quote:
Originally Posted by vart View Post
you haven't asked and OP haven't said if it is Windows or Unix

you do not check return value of scanf...
you do not guard against buffer overrun in scanf
and void main - read FAQ why you should not use it
Hi Vart, I am using Unix.

Here are portions of my code:

Code:
void* scan(char* buffer)
{
        return fgets(buffer, MAX_BUFFER_SIZE, stdin);
}

void dump_scanf(FILE* fp)
{
        int ch;
        while( (ch = fgetc(fp)) != EOF && ch != '\n' );
}
Code:
                int x,y;
                char extra;
                char* buffer = (char *)malloc(sizeof(char)*MAX_BUFFER_SIZE);

                printf("Your input: ");
                void* scan_ptr = scan(buffer);
                if(scan_ptr)
                {
                        r = sscanf(buffer, "%d %d%c", &x, &y, &extra);

                        printf("feof(stdin)=%d\n", feof(stdin));
                        if(strlen(buffer) > 6)
                        {
                                dump_scanf(stdin);
                        }
                }
Code:
Your input: 2 2^D
feof(stdin)=0
^D is shown when I press the combination keys Ctrl+D. How do I read that?

Last edited by CashCow01; 03-21-2010 at 12:25 AM.
CashCow01 is offline   Reply With Quote
Old 03-21-2010, 12:34 AM   #8
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,556
feof flag will be set after the read failed...

so something like this should be used

Code:
while(fgets( buffer, sizeof buffer, stdin))
{
 /* process buffer */
}

if(feof(stdin))
{
   puts("EOF detected\n");
}
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation fault ith g_type_check_class_cast () from /usr/lib/libgobject-2.0.so.0 lehe Game Programming 7 02-02-2009 07:27 AM
reading integers using fgets larne C Programming 3 10-15-2008 11:36 AM
An fgets() implementation using ReadFile() bodom Windows Programming 1 10-03-2004 11:27 PM
Array, reading in response etc... mattz C Programming 4 12-05-2001 11:41 AM
Reading from a file *pointer C Programming 5 10-17-2001 07:44 PM


All times are GMT -6. The time now is 12:17 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22