| | |
Scansets
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
I'm trying to use scansets to filter out out some textfiles.[/COLOR]
When I input "123abcdtye" I expect the output to be "123 abcd tye". Instead my program exits as if it reaches the return statement even though I have put in a getchar() to pause the program. What is going on?
C Syntax (Toggle Plain Text)
#include <stdio.h> int main( void) { int i; char str[ 80], str2[ 80]; scanf("%d %[abcdefg] %s", &i, str, str2); printf("%d %s %s", i, str, str2); getchar(); return 0; }
When I input "123abcdtye" I expect the output to be "123 abcd tye". Instead my program exits as if it reaches the return statement even though I have put in a getchar() to pause the program. What is going on?
Last edited by Dave Sinkula; Aug 30th, 2006 at 9:30 pm. Reason: fixed format and deleting some spaces added when pasting ## Fixed the fix.
Ok that's weird. Using system("PAUSE") gives me the correct output but why is this? It seems to me that they should be equivalent in this situation but apparently I have a misunderstanding.
I've been taught to use getchar() over system("PAUSE)http://www.gidnetwork.com/b-61.html
I'm using Dev-C++
I've been taught to use getchar() over system("PAUSE)http://www.gidnetwork.com/b-61.html
I'm using Dev-C++
Last edited by VinC; Aug 30th, 2006 at 1:08 pm. Reason: include compiler
No, don't. Learn your tools. Using
Use a combination of
[edit]Since you already learned this from GID, you could check out the
scanf() to read characters is even worse than using gets() because not only does it allow you to blow your buffer boundaries but as you can see it also leaves your input stream dirty. The \n is left in the stream for your getchar() to read.Use a combination of
fgets() and sscanf() for safety and to process the input stream cleanly. C Syntax (Toggle Plain Text)
fgets(tmpstr, 80, stdin); sscanf(tmpstr,"%d %[abcdefg] %s", &i, str, str2);
scanf() series there. [/edit] Last edited by WaltP; Aug 30th, 2006 at 1:25 pm.
> gives me the correct output but why is this?
Because scanf() is full of wonderful surprises - the most usual being that it leaves the \n on the input stream - just ready for the getchar() to return with immediate success.
I'd suggest you ALWAYS use fgets() to read a line of input, then use sscanf() (or something else) to extract information from the buffer.
fgets() leaves the input stream in a far more consistent state than scanf() ever can.
Because scanf() is full of wonderful surprises - the most usual being that it leaves the \n on the input stream - just ready for the getchar() to return with immediate success.
I'd suggest you ALWAYS use fgets() to read a line of input, then use sscanf() (or something else) to extract information from the buffer.
fgets() leaves the input stream in a far more consistent state than scanf() ever can.
C Syntax (Toggle Plain Text)
char buff[BUFSIZ]; if ( fgets( buff, sizeof buff, stdin ) != NULL ) { if ( sscanf( buff, "%d %[abcdefg] %s", &i, str, str2) == 3 ) { printf("%d %s %s\n", i, str, str2); } else { // some error } }
[edit]Since you already learned this from GID, you could check out the
Will do. I couldn't help but notice that the author of the articles on GID is also WaltP! :eek:
scanf() series there. [/edit][/quote] Will do. I couldn't help but notice that the author of the articles on GID is also WaltP! :eek:
![]() |
Other Threads in the C Forum
- Previous Thread: Help with some parts of this pseudocode
- Next Thread: SOS help
Views: 2095 | Replies: 12
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays asterisks bash binarysearch calculate changingto char character cm command copyimagefile creafecopyofanytypeoffileinc createprocess() database directory dynamic execv feet fgets file floatingpointvalidation fork forloop framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux hacking histogram homework ide include incrementoperators input intmain() iso kernel keyboard kilometer lazy license linked linkedlist linux list lists looping loopinsideloop. lowest matrix meter microsoft mqqueue number oddnumber odf openwebfoundation overwrite pause pdf pointer posix process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprogramming spoonfeeding standard strchr string student system testing threads turboc unix urboc user variable whythiscodecausesegmentationfault windowsapi






