![]() |
| | #1 |
| Registered User Join Date: Sep 2003
Posts: 6
| Stopping input with CTRL+D 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 | |
| | #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 | |
| | #3 |
| 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 | |
| | #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 | |
| | #5 |
| 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |