469,626 Members | 1,610 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

vsscanf()

I have always used istringstream for inputting data from a string.

But I heard someone talking about vsscanf()

However from what I can find in the internet is not quite clear if is
standard or not (some say it is, some say it isn't)

Also it's very confusing how to use it.
I have used the va_ combo before: va_list, va_start(), va_arg(),
va_end()

However it´s not clear if I need to use va_start() or not, how I
should I define to stop the reading process: is vssscanf() supposed to
return something as EOF.

Thanks.

May 11 '07 #1
4 2281
On May 11, 1:43 pm, Gaijinco <gaiji...@gmail.comwrote:
I have always used istringstream for inputting data from a string.

But I heard someone talking about vsscanf()

However from what I can find in the internet is not quite clear if is
standard or not (some say it is, some say it isn't)

Also it's very confusing how to use it.
I have used the va_ combo before: va_list, va_start(), va_arg(),
va_end()

However it´s not clear if I need to use va_start() or not, how I
should I define to stop the reading process: is vssscanf() supposed to
return something as EOF.
Using stringstreams is generally preferable in C++, but vsscanf (and
other v*f sibilings) are Standard. It's basically a back end for
sscanf, which is the old C way to do what stringstreams do now, and I
found several examples by googling. My question is why do you want to
use it? It is not type-safe, which is usually bad in C++.

Cheers! --M

May 11 '07 #2
On 2007-05-11 19:43, Gaijinco wrote:
I have always used istringstream for inputting data from a string.

But I heard someone talking about vsscanf()

However from what I can find in the internet is not quite clear if is
standard or not (some say it is, some say it isn't)
vsscanf is part of C99 and will thus probably become part of C++09, the
closest you'll get in current C++ is sscanf (found in <cstdio>). If you
have a C99 compliant compiler/library you'll find vsscanf in <stdarg.h>.

Personally I would go with the stringstream unless you have some
compelling reason not to do so.

--
Erik Wikström
May 11 '07 #3
I just really curiosity.

I have google it and it's true that there are lot of pages with info
about it, but it´s really vague.

In all the pages they said that you need to use a variable of type
va_list but there is no indication of to what extend.

This all began because I want to read a string and read numbers on it.
Something like:

"1 2 3 4 5"

So I did something like:

char sequence[100];
gets(sequence);

But this is the case where it gets confusing, I would expect to do
something like:

va_list args;
int n = vsscanf(sequence, "%d", args);

But everytime I call vsscanf() it would return 1 (I suppose akin to
true), but I can never read the numbers of the sequence.

I understand that istringstream maight be better, but it´s just for
curiosity-sake.

Thanks.

May 12 '07 #4
On 2007-05-12 05:09, Gaijinco wrote:
I just really curiosity.

I have google it and it's true that there are lot of pages with info
about it, but it´s really vague.

In all the pages they said that you need to use a variable of type
va_list but there is no indication of to what extend.

This all began because I want to read a string and read numbers on it.
Something like:

"1 2 3 4 5"

So I did something like:

char sequence[100];
gets(sequence);

But this is the case where it gets confusing, I would expect to do
something like:

va_list args;
int n = vsscanf(sequence, "%d", args);

But everytime I call vsscanf() it would return 1 (I suppose akin to
true), but I can never read the numbers of the sequence.
Please quote the text you are replying to.

In the future when you are wondering how to use a C or POSIX function
remember that most Linux/BSD systems comes with man pages describing
them, personally I find OpenBSDs man pages to be of good quality, for
vsscanf look here:

http://www.openbsd.org/cgi-bin/man.cgi?query=vsscanf

There you can read that the function returns the number of input items
assigned, which means that your example above worked correctly. However
if you want to read more than on digit you need to specify that in the
formate string:

va_list args;
int n = vsscanf(sequence, "%d %d %d %d %d", args);

Should give you n == 5 and the values read should be stored in args.

--
Erik Wikström
May 12 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

47 posts views Thread by Vijay Kumar R Zanvar | last post: by
2 posts views Thread by RoSsIaCrIiLoIA | last post: by
2 posts views Thread by Ray Mitchell | last post: by
7 posts views Thread by Ray Mitchell | last post: by
62 posts views Thread by Juuso Hukkanen | last post: by
2 posts views Thread by david wolf | last post: by
8 posts views Thread by Fred | last post: by
16 posts views Thread by Chad | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.