Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > sscanf and scanf behave differently

Reply
Thread Tools

sscanf and scanf behave differently

 
 
effbiae
Guest
Posts: n/a
 
      01-19-2006
is sscanf meant not to consume chars? run this program:

/*ss.c*/
int main(int argc,char *argv[])
{char buf[3];
int i;for(i=0;i<3;i++)
{printf("ss: %d %s\n",sscanf(argv[1],"%2s",buf),buf);
printf(" s: %d %s\n",scanf("%2s",buf),buf);
}}


with the command:

$ gcc ss.c && echo 1234567890 |./a.out 1234567890
ss: 1 12
s: 1 12
ss: 1 12
s: 1 34
ss: 1 12
s: 1 56

note that the progress of ss differs to that of s.

kind regards,


jack

 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      01-19-2006
effbiae wrote:

> is sscanf meant not to consume chars?


Yes.

--
Chris "understanding is a three-edged sword" Dollin
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      01-19-2006
"effbiae" <(E-Mail Removed)> writes:
> is sscanf meant not to consume chars? run this program:

[snip]

scanf() reads from stdin. If some input has already been read, it
won't be read again (unless you use ungetc()).

sscanf() starts scanning from the beginning of the string you pass to
it. It doesn't keep track internally of what it did last time. If
you wnat sscanf() to start scanning from the middle of a string, you
need to pass it a pointer into the middle of the string.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
multithreading ; linux adn windows behave differently kumarchi@gmail.com C Programming 9 05-17-2008 09:22 AM
same html code behave differently in IE and FireFox usgog Javascript 2 03-24-2007 12:13 AM
C++ Exception: Why does this program behave differently for pointer and array of pointer Divick C++ 7 09-12-2005 05:49 AM
Why do #dup and #clone behave differently with respect to instance methods Robert Klemme Ruby 5 05-18-2004 09:21 AM
Do threads behave Differently on Windows 98? Codemonkey ASP .Net 1 10-30-2003 04:16 AM



Advertisments