![]() |
| | #1 |
| Registered User Join Date: Mar 2010
Posts: 7
| Reading Ctrl D in fgets with some inputs 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 | |
| | #2 |
| Registered User 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 | |
| | #3 |
| Registered User Join Date: Feb 2010 Location: London, United Kingdom
Posts: 1,062
| |
| claudiu is offline | |
| | #4 |
| Registered User Join Date: Mar 2010
Posts: 14
| K always 4 then, how about a solution?? |
| magestrium is offline | |
| | #5 |
| CSharpener 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 | |
| | #6 |
| Registered User 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 | |
| | #7 | |
| Registered User Join Date: Mar 2010
Posts: 7
| Quote:
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 Last edited by CashCow01; 03-21-2010 at 12:25 AM. | |
| CashCow01 is offline | |
| | #8 |
| CSharpener 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |