vsscanf()

G

Gaijinco

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.
 
M

mlimber

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
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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.
 
G

Gaijinco

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.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top