473,320 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 2496
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

47
by: Vijay Kumar R Zanvar | last post by:
Did you know that the following if statement is semantically right, but could logically be wrong? if ( i = 2 ) { /* do something */ } Tip:
2
by: RoSsIaCrIiLoIA | last post by:
Is it good the use of va_arg? ____________________ #include <stdio.h> #include <stdlib.h> #include <stdarg.h> #include <errno.h> unsigned gcd_my(unsigned a, unsigned b) {unsigned t;
2
by: Ray Mitchell | last post by:
Hello, I realize that the source code for vsscanf is available from several sources, such as GNU. However, all such source code I've found so far is filled with cryptic (to me) #ifdefs, system...
7
by: Ray Mitchell | last post by:
Hello, I realize that the source code for vsscanf is available from several sources, such as GNU. However, all such source code I've found so far is filled with cryptic (to me) #ifdefs, system...
4
by: smshahriar | last post by:
Hi, I want to scan from the following string all the hex numbers and populate an array of integers: 0x27 0x00 0x30 0x00 0x33 0x00 0x36 0x00
62
by: Juuso Hukkanen | last post by:
I am looking for a wish list of things which should be removed from the C (C99) - due to feature's bad security track record <OT>or Multithreading unsafety. I need this list for a project intending...
2
by: david wolf | last post by:
My understanding is that cstdio basically is the same as stdio.h except the functions are in a namspace called std. However when I take a look at the content of the file cstdio, it has the...
8
by: Fred | last post by:
I've got following program encapsuled fscanf, however, it doesn't work. I'm sure that the content format in "a.txt" is OK, the content would be correctly read if using fscanf directly, what's...
16
by: Chad | last post by:
Given the following #include <stdio.h> int main(void) { char line; long arg1, arg2; while(fgets(line, BUFSIZ, stdin) != NULL){
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.