Stopping input with CTRL+D

This is a discussion on Stopping input with CTRL+D within the Linux Programming forums, part of the Platform Specific Boards category; I'm trying to write a program in linux that will allow me to enter a word then a space then ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    6

    Stopping input with CTRL+D

    I'm trying to write a program in linux that will allow me to enter a word then a space then a number. Then after I hit enter it will let me do this again and again. Now I need to be able to stop this so I want it to stop taking input and do some processing when I hit CTRL+D. Something like this:

    joe 30
    bob 40
    jill 20
    (CTRL+D pressed)
    (now do stuff with the data just entered)


    But I can't seem to get it to work without getting a core dump. BTW im writing this in C not C++. Any one know how I can do this? I sorry I don't have the code I have, I left it on another computer that I can't access right now. Thanks for any suggestions. I'm rather new at linux developing.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    51
    Ctrl+D == EOF

    use getchar to get the characters from input and scan for EOF character.

    input is buffered so you can use a while loop with getchar to get a input string.

  3. #3
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    Sounds like you need an fgets/sscanf pair and a linked list:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct node {
      char name[100];
      int number;
      struct node *next;
    } *link;
    
    int main ( ) {
      char buffer[BUFSIZ];
      link list;
      link scratch;
    
      list = 0;
      while (fgets(buffer, sizeof buffer, stdin) != NULL) {
        if ((scratch = malloc(sizeof *scratch)) == NULL)
          break;
        if (sscanf(buffer, "%s%d", scratch->name, &scratch->number) != 2)
          break;
        scratch->next = list;
        list = scratch;
      }
      for (scratch = list; scratch != NULL; scratch = scratch->next)
        printf("%s -- %d\n", scratch->name, scratch->number);
    
      return 0;
    }
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    6
    Great, thanks a lot guys! I was able to get this to work using visual C++ in a console app but with linux it seemed different. There were some functions I used before that my linux compiler did not know about so I wasn't sure what to do. It's been ages since I used C last so I forgot some of the functions. Thanks!

  5. #5
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    >There were some functions I used before that my linux compiler did not know about
    Sounds like a case of Microsoftitis. Take two of these (ISO/IEC 9899:1999) and call me in the morning. I also recommend a strict regimen of Linux use for the next 50 years. Try to stay off Windows as well, it's bad for your mental health.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input redirection
    By sashaKap in forum C Programming
    Replies: 6
    Last Post: 06-25-2009, 02:59 AM
  2. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 04:54 AM
  3. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 01:22 AM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  5. Help with Input Checking
    By Derek in forum C Programming
    Replies: 7
    Last Post: 06-17-2003, 04:07 AM

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