Reading Ctrl D in fgets with some inputs

This is a discussion on Reading Ctrl D in fgets with some inputs within the C Programming forums, part of the General Programming Boards category; Hi to all, I was wondering if you guys can help me with this tricky stdin problem? Suppose I want ...

  1. #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.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    As far as I know CTRL+D is actually EOF. So test for EOF.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    It's always 4.

    check out this useful link:

    Ascii control codes (control characters, C0..controls)

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    14
    K always 4 then, how about a solution??

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.

  7. #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.

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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");
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  2. reading integers using fgets
    By larne in forum C Programming
    Replies: 3
    Last Post: 10-15-2008, 11:36 AM
  3. An fgets() implementation using ReadFile()
    By bodom in forum Windows Programming
    Replies: 1
    Last Post: 10-03-2004, 11:27 PM
  4. Array, reading in response etc...
    By mattz in forum C Programming
    Replies: 4
    Last Post: 12-05-2001, 10:41 AM
  5. Reading from a file
    By *pointer in forum C Programming
    Replies: 5
    Last Post: 10-17-2001, 07:44 PM

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