Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > specifier "%n" when using "scanf"

Reply
Thread Tools

specifier "%n" when using "scanf"

 
 
thomas
Guest
Posts: n/a
 
      08-18-2009
Hi,
I used to see a format specifier "%s%n" for "scanf", like
follows.

------------code---------
#include<iostream>
using namespace std;

int main(){
char xx[20];
strcpy(xx, "ab ad ef");
char yy[20]; int jj;
cout<<sscanf(xx, "%s%n", yy, &jj)<<endl;
cout<<yy<<" "<<jj<<endl;
}
--------code-----------

But I didn't find any explaination about the "%n" usage in the web or
text books.
Can anyone give some references?
 
Reply With Quote
 
 
 
 
Maxim Yegorushkin
Guest
Posts: n/a
 
      08-18-2009
thomas wrote:
> Hi,
> I used to see a format specifier "%s%n" for "scanf", like
> follows.
>
> ------------code---------
> #include<iostream>
> using namespace std;
>
> int main(){
> char xx[20];
> strcpy(xx, "ab ad ef");
> char yy[20]; int jj;
> cout<<sscanf(xx, "%s%n", yy, &jj)<<endl;
> cout<<yy<<" "<<jj<<endl;
> }
> --------code-----------
>
> But I didn't find any explaination about the "%n" usage in the web or
> text books.


It returns the number of bytes consumed by sscanf by that point, provided it
gets to that point.

> Can anyone give some references?


http://www.opengroup.org/onlinepubs/...ons/scanf.html

--
Max
 
Reply With Quote
 
 
 
 
Alexander Bartolich
Guest
Posts: n/a
 
      08-18-2009
thomas wrote:
> Hi,
> I used to see a format specifier "%s%n" for "scanf", like
> follows.


C++1998 is based on C90, so technically scanf is part of C++.
Nevertheless the function is described by the C standard and
is rarely mentioned in literature about C++.

> [...]
> But I didn't find any explaination about the "%n" usage in the
> web or text books.


Hard to believe.

> Can anyone give some references?


http://man-wiki.net/index.php/3:fscanf
# n Nothing is expected; instead, the number of characters consumed
# thus far from the input is stored through the next pointer, which
# must be a pointer to int. This is not a conversion, although it can
# be suppressed with the * assignment-suppression character. The C
# standard says: "Execution of a %n directive does not increment the
# assignment count returned at the completion of execution" but the
# Corrigendum seems to contradict this. Probably it is wise not to make
# any assumptions on the effect of %n conversions on the return value.

http://msdn.microsoft.com/en-us/libr...hh(VS.71).aspx
# n No input read from stream or buffer.
# Pointer to int, into which is stored number of characters successfully
# read from stream or buffer up to that point in current call to scanf
# functions or wscanf functions.

--
Brüder, in die Tonne die Freiheit,
Brüder, ein Stoppschild davor.
Egal was die Schwarzen Verlangen
Rufen wir: Ja! Brav im Chor.
 
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
printf function changes int to char when using format specifier? vlsidesign C Programming 2 11-14-2007 09:50 PM
limiting string size in data grid format specifier for column =?Utf-8?B?UGF1bA==?= ASP .Net 1 02-01-2005 12:47 AM
Attribute specifier is not a complete statement eror Justin Dutoit ASP .Net 1 10-05-2004 07:56 AM
Access specifier and inheritance mshetty C++ 3 02-17-2004 03:08 PM
overloding and const specifier Kiran Shastry C++ 1 07-18-2003 11:09 AM



Advertisments