sscanf and string

G

Giff

Hello,

I have spent about one hour like an idiot on this stupid piece of code:

int main(int argc, char *argv[])
{
int a;
float b;
string c;
int res = sscanf( "20 10.0 hello", "%d %f %s", &a, &b, &c);
printf("%d %f\n", a, b); //fine
printf("%s", c); //->segfault
return 0;
}

As the comment says, it segfaults at the second printf.

I suppose I can't assign to a string using sscanf? Thing is that I
thought I saw it done like that somewhere on the net...

What puzzles me most is: why does it crash only when I try to print the
string?
How would you do it without having to resort to char arrays?

Thanks,
G
 
Ö

Öö Tiib

Hello,

I have spent about one hour like an idiot on this stupid piece of code:

int main(int argc, char *argv[])
{      
        int a;
        float b;
        string c;
        int res = sscanf( "20 10.0 hello", "%d %f %s", &a, &b, &c);
        printf("%d %f\n", a, b); //fine
        printf("%s", c); //->segfault
        return 0;

}

As the comment says, it segfaults at the second printf.

I suppose I can't assign to a string using sscanf? Thing is that I
thought I saw it done like that somewhere on the net...

What puzzles me most is: why does it crash only when I try to print the
string?
How would you do it without having to resort to char arrays?

scanf() is C function. Use std::cin in C++. std::string is C++ string.
How you expect old C function (that expects char*) to read into
std::string?
 
F

Francesco S. Carta

Hello,

I have spent about one hour like an idiot on this stupid piece of code:

int main(int argc, char *argv[])
{
int a;
float b;
string c;
int res = sscanf( "20 10.0 hello", "%d %f %s", &a, &b, &c);
printf("%d %f\n", a, b); //fine
printf("%s", c); //->segfault
return 0;
}

As the comment says, it segfaults at the second printf.

I suppose I can't assign to a string using sscanf? Thing is that I
thought I saw it done like that somewhere on the net...

What puzzles me most is: why does it crash only when I try to print the
string?

If that "string" is a std::string, then that string is getting
corrupted. When you sscanf some text, you need pass a char buffer to it,
not a std::string.
How would you do it without having to resort to char arrays?

Simply forget about sscanf and printf and use standard streams along
with the "<<" ">>" operators. It's quite a different approach from C
style string parsing, so you might take advantage of reading some manuals.

And reading the C++ FAQ will be a good advantage for the next postings
too, your current post didn't give enough information to definitely
answer your question.

Have fun
 
G

Giff

Il 19/07/2010 22:11, Francesco S. Carta ha scritto:
And reading the C++ FAQ will be a good advantage for the next postings
too, your current post didn't give enough information to definitely
answer your question.

Thanks to both for the valuable information, and yes, the post did lack
some info and the code was not directly compilable, my bad.

Cheers,
G
 
F

Francesco S. Carta

Il 19/07/2010 22:11, Francesco S. Carta ha scritto:


Thanks to both for the valuable information, and yes, the post did lack
some info and the code was not directly compilable, my bad.

You're welcome, have good time with C++
 
J

Juha Nieminen

Giff said:
string c;
int res = sscanf( "20 10.0 hello", "%d %f %s", &a, &b, &c);

The "%s" is telling sscanf to expect a char*, but instead you are giving
it an address to an object of type std::string, which is certainly not the
same thing. Depending on your compiler, if you turn enough warnings on, it
will tell you that.

sscanf() is a standard C function and has no idea whatsoever what
"std::string" might be. It cannot handle such things. (And of course it
doesn't help that by telling it "%s" it's expecting a char*.)

What you want is this:

int i;
double d;
std::string s;
std::istringstream is("20 10.0 hello");
is >> i >> d >> s;
std::cout << i << " " << d << " " << s << "\n";
What puzzles me most is: why does it crash only when I try to print the
string?

Because sscanf() is corrupting memory, and anything can happen.
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top