Skip to main content
GameDev.net gamedev.net
🔒 Locked

std::string problems / C++

Started by PhiberOptic Jan 1, 2004 at 1:24 PM 20 replies 3k views
Original Post
PhiberOptic
PhiberOptic
Okay, I got a function that uses std::string a lot for some operations. This is the function: bool MyClass::func() { std::string buffer; while( something ) { buffer = bla bla // lots of "buffer =" here.. //then in the end of the function I have these lines: buffer = readLine(fin); sscanf(buffer.c_str(),"%f %f %f %f", &b->rot.w, &b->rot.x, &b->rot.y, &b->rot.z); //Break! Here buffer starts to act wierd! } } When it passes that sscanf function, the buffer variable starts to act wierd. No matter what you do with it, all it does is make itself equals "D". I''ve tried a lot of stuff like: buffer = "asdf"; buffer = szSomeString; buffer = otherStdString; buffer = readLine(); buffer.clear(); buffer = std::string("hello world"); Nothing helped.. buffer still equals "D".. :-( Does anyone know anything about this behaviour? ---------------------------------------------- Petter Nordlander "There are only 10 kinds of people in the world. They who understand binary and those who do not"
----------------------------------------------Petter Nordlander"There are only 10 kinds of people in the world. They who understand binary and those who do not"
Fruny
Fruny

#include <iostream>
#include <string>
#include <sstream>

void func()
{
std::string buffer;
std::stringstream ss;

while( condition() )
{
std::getline(fin, buffer);
ss.str(buffer)
ss >> b->rot.w >> b->rot.x >> b->rot.y >> b->rot.z;
ss.clear(); // clears the EOF, fail, bad... flags

}
}


You can use stream member functions like fail() to check whether any given IO operation failed.

[edited by - Fruny on January 1, 2004 2:35:29 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
PhiberOptic
PhiberOptic
I think you got me wrong. The input didn''t fail. It was good. The problem is that the "buffer" variable can''t be used after that line. When I try to assign a string to it, it only assigns a "D".

----------------------------------------------
Petter Nordlander

"There are only 10 kinds of people in the world. They who understand binary and those who do not"
----------------------------------------------Petter Nordlander"There are only 10 kinds of people in the world. They who understand binary and those who do not"
Fruny
Fruny
What do you use to show that "D" ?
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
PhiberOptic
PhiberOptic
Visual Studio.NET debuger.. using breakpoints and stuff..

----------------------------------------------
Petter Nordlander

"There are only 10 kinds of people in the world. They who understand binary and those who do not"
----------------------------------------------Petter Nordlander"There are only 10 kinds of people in the world. They who understand binary and those who do not"
Premandrake
Premandrake
Are you possibly debugging in release mode? I''ve done that more than a few times and it gives very strange results.

From the code I''ve posted I don''t see how it would be incorrect, everything seems fine.
Fruny
Fruny

#include <cstdio>
#include <iostream>

int main()
{
float a,b,c,d;
std::string buffer = "1000 1000 1000 1000";
std::sscanf( buffer.c_str(), "%f %f %f %f", &a, &b, &c, &d);
std::cout << a << ' ' << ' ' << b << ' ' << c << ' ' << d << std::endl;
}


Works without a hitch for me (VS 7.1)

[edited by - Fruny on January 1, 2004 2:59:02 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
PhiberOptic
PhiberOptic
of course it does. I have plenty of similar code too.

Now converted to stringstreams instead of sscanf..

// Loc

buffer = readLine(fin);
ss.str(buffer);
ss >> b->loc.x >> b->loc.y >> b->loc.z;
ss.clear();
// Buffer works

// Head

buffer = readLine(fin);
ss.str(buffer);
ss >> b->head.x >> b->head.y >> b->head.z;
ss.clear();
// Buffer still works..

// Tail

buffer = readLine(fin);
ss.str(buffer);
ss >> b->tail.x >> b->tail.y >> b->tail.z;
ss.clear();
// buffer still works

// Quat

buffer = readLine(fin);
ss.str(buffer);
ss >> b->rot.w >> b->rot.x >> b->rot.y >> b->rot.z;
ss.clear();
// Buffer don''t work!





----------------------------------------------
Petter Nordlander

"There are only 10 kinds of people in the world. They who understand binary and those who do not"
----------------------------------------------Petter Nordlander"There are only 10 kinds of people in the world. They who understand binary and those who do not"
Fruny
Fruny
No clue...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Zipster
Zipster
Fruny not having a clue is a sign that the Apocalypse is near...

It could be something specifically with the b->rot object causing sscanf to fail. Or even the stringstreams. I used to notice strange behavior whenever the types were mismatched.
PhiberOptic
PhiberOptic
quote:
Original post by Zipster
It could be something specifically with the b->rot object causing sscanf to fail. Or even the stringstreams. I used to notice strange behavior whenever the types were mismatched.


Shouldn''t be.. The b->rot seem to work perfectly and with all variables set correctly. It has to be something with the input I do to buffer. but why can that mess it up?



----------------------------------------------
Petter Nordlander

"There are only 10 kinds of people in the world. They who understand binary and those who do not"
----------------------------------------------Petter Nordlander"There are only 10 kinds of people in the world. They who understand binary and those who do not"
emileej
emileej
I am quite clueless here, but try creating a char buffer for use in the scanf and then assign the value to your string. If no other reason - do it for kicks.
Emil Johansen- SMMOG AI designerhttp://smmog.com
cgoat
cgoat
Was "b" properly allocated?

Have you tried not looking at it in the debugger? Just have your code print out the value to the screen. (buffer = "Hello"; cout << buffer; ) Those STL classes do some strange stuff, and I've found it difficult to look at them in the debugger with Visual C++, though I haven't tried .NET ever.

[edited by - cgoat on January 2, 2004 11:51:18 AM]
Pipo DeClown
Pipo DeClown
Structure of b? Can you show us that?

--
You''re Welcome,
Rick Wong

- sitting in his chair doing the most time-consuming thing..
Daniel Benmergui
Daniel Benmergui
Well, the STL documentation regarding c_str states that:
"The returned C-style string should not be modified, as this could invalidate the pointer to the string, or deleted, as the string has a limited lifetime and is owned by the class string."

So you shouldn''t modify it

Assign the new value to a TCHAR/char/whatever buffer, sscanf it, and assign it to the STL string

Good Luck,
Daniel.
cgoat
cgoat
He''s not changing the string returned by c_str.
Kylotan
Kylotan
Ok... so you have two almost identical blocks of code, one for Tail, one for Quat. And after the 2nd, you say that buffer stops working. However I notice you''ve not checked the status of buffer on each of those 4 lines. eg.

- what is the return value of readLine
- what is buffer after it''s been assigned to ss
- what is buffer after you''ve read the 4 values out of ss

Maybe examining those will shed some light on things? After all, I don''t know what your readLine function does, or why its needed when there are existing line-reading functions in the library. Perhaps it returns an invalid string somehow.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
Hull
Hull
Are b->rot.w, x, y, z double data types?

If so, change them to floats.

"Self awareness is the interaction between 3 different parts of your brain. The Cog, the Left and the Right side of you brain..."
MauMan
MauMan
If you''re using VS 2002 there''s a bug in std::string''s evaluation in the debugger. If the string is less that 16 characters (I may be wrong with this number) it stores the string in a static buffer. This is what the debugger shows. The first time you exceed that buffer length it puts the string in a dynamically-allocated buffer. If you want to look at what''s in it you have to expand the string to look at its members.

---CyberbrineDreamsSuspected implementation of the Windows idle loop: void idle_loop() { *((char*)rand()) = 0; }

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.