Scansets

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2006
Posts: 16
Reputation: VinC is an unknown quantity at this point 
Solved Threads: 0
VinC's Avatar
VinC VinC is offline Offline
Newbie Poster

Scansets

 
0
  #1
Aug 30th, 2006
I'm trying to use scansets to filter out out some textfiles.[/COLOR]

  1. #include <stdio.h>
  2. int main( void)
  3. {
  4. int i;
  5. char str[ 80], str2[ 80];
  6. scanf("%d %[abcdefg] %s", &i, str, str2);
  7. printf("%d %s %s", i, str, str2);
  8. getchar();
  9. return 0;
  10. }

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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 155
Reputation: mostafadotnet is on a distinguished road 
Solved Threads: 10
mostafadotnet's Avatar
mostafadotnet mostafadotnet is offline Offline
Junior Poster

Re: Scansets

 
0
  #2
Aug 30th, 2006
Hi VinC,
#include <stdio.h>
int main( void)
{
int i;
char str[ 80], str2[ 80];
scanf("%d %[abcdefg] %s", &i, str, str2);
printf("%d %s %s\n", i, str, str2);
system("pause");
return 0;
}
Use
system("pause");
instead.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 147
Reputation: Grunt has a spectacular aura about Grunt has a spectacular aura about 
Solved Threads: 12
Grunt's Avatar
Grunt Grunt is offline Offline
Junior Poster

Re: Scansets

 
0
  #3
Aug 30th, 2006
Code is fine and it works as expected in my compiler(gcc 3.4.5).
Try using some other input mechanism which doesn't leave stuff back in the input stream.
Last edited by Grunt; Aug 30th, 2006 at 1:11 pm.
The key to eliminating bugs from your code is learning from your mistakes.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 16
Reputation: VinC is an unknown quantity at this point 
Solved Threads: 0
VinC's Avatar
VinC VinC is offline Offline
Newbie Poster

Re: Scansets

 
0
  #4
Aug 30th, 2006
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++
Last edited by VinC; Aug 30th, 2006 at 1:08 pm. Reason: include compiler
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 16
Reputation: VinC is an unknown quantity at this point 
Solved Threads: 0
VinC's Avatar
VinC VinC is offline Offline
Newbie Poster

Re: Scansets

 
0
  #5
Aug 30th, 2006
Did it manage to work with getchar() or system("PAUSE")?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,121
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Scansets

 
1
  #6
Aug 30th, 2006
Originally Posted by mostafadotnet View Post
Hi VinC,
system("pause");
Use instead.
No, don't. Learn your tools. Using 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.
  1. fgets(tmpstr, 80, stdin);
  2. sscanf(tmpstr,"%d %[abcdefg] %s", &i, str, str2);
[edit]Since you already learned this from GID, you could check out the scanf() series there. [/edit]
Last edited by WaltP; Aug 30th, 2006 at 1:25 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 16
Reputation: VinC is an unknown quantity at this point 
Solved Threads: 0
VinC's Avatar
VinC VinC is offline Offline
Newbie Poster

Re: Scansets

 
0
  #7
Aug 30th, 2006
Thank you for all the help!
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Scansets

 
0
  #8
Aug 30th, 2006
> 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.

  1. char buff[BUFSIZ];
  2. if ( fgets( buff, sizeof buff, stdin ) != NULL ) {
  3. if ( sscanf( buff, "%d %[abcdefg] %s", &i, str, str2) == 3 ) {
  4. printf("%d %s %s\n", i, str, str2);
  5. } else {
  6. // some error
  7. }
  8. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 16
Reputation: VinC is an unknown quantity at this point 
Solved Threads: 0
VinC's Avatar
VinC VinC is offline Offline
Newbie Poster

Re: Scansets

 
0
  #9
Aug 30th, 2006
[edit]Since you already learned this from GID, you could check out the 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:
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 155
Reputation: mostafadotnet is on a distinguished road 
Solved Threads: 10
mostafadotnet's Avatar
mostafadotnet mostafadotnet is offline Offline
Junior Poster

Re: Scansets

 
0
  #10
Aug 30th, 2006
Hi all,
I've just suggested the easiest and the worst way to use when you have windows running.
Simply,i don't recommend it for your commercial&/ important projects.

Good luck.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum


Views: 2095 | Replies: 12
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC