C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-19-2003, 03:39 PM   #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.
SirKnightTG is offline   Reply With Quote
Old 09-20-2003, 02:00 AM   #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.
Kyro is offline   Reply With Quote
Old 09-20-2003, 07:54 AM   #3
twm
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.
twm is offline   Reply With Quote
Old 09-20-2003, 11:52 AM   #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!
SirKnightTG is offline   Reply With Quote
Old 09-20-2003, 11:58 AM   #5
twm
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.
twm is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:47 PM.


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