questionsolved  ostringstream doesn't work with string and integer

bob10 (4)   Link to this post
Hi there,
i'm trying to use a ostringstream to insert two integers in a string.
This is my code at the moment:
1
2
3
4
5
6
7
8
9
bool SimuClient::bInitialisiereGrafik(int sizeX, int sizeY, bool bStarteServer) {
	ostringstream ssNachricht;
	
	// Init-Befehl senden
	ssNachricht << "init " << sizeX << " " << sizeY << "#";
	cout << ssNachricht.str() << endl;
	cout << "size() = " << ssNachricht.str().size() << endl;
        return false;
}


The output of this function is:

init
size() = 5

Does anyone know why the first integer isn't appended to the ostringstream?
My debugger says that "init " is followed by a \0. Why?!

Kind regards,
bob
Last edited on
jjlimax (2)   Link to this post
This works for me. Try check your compiler settings. It is also possible that you have a strange sstream library.
bob10 (4)   Link to this post
I use gcc version 4.2.1 (Apple Inc. build 5646) on MacOS 10.6.1.

In my desperation I wrote another test program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <sstream>
using namespace std;

int main() {
    ostringstream ss;

    cout << "Ausgabe:" << endl;
    ss << "init " << 5 << " " << 6 << "#";

    cout << ss.str() << endl;
    cout << "size() = " << ss.str().size() << endl;

    return EXIT_SUCCESS;
}


and this works!

... confusing?!
Last edited on
MaikCAE (16)   Link to this post
I had nearly such a problem sometimes but I can't really remember how I fixed it.

Did you also try

1
2
3
4
5
ssNachricht << "init ";
ssNachricht << sizeX;
ssNachricht << " " ;
ssNachricht << sizeY; 
ssNachricht << "#";


or

1
2
3
int sX = sizeX;
int sY = sizeY;
ssNachricht << "init " << sX << " " << sY << "#";
Bazzy (3168)   Link to this post
Try flushing the stringstream
bob10 (4)   Link to this post
The problem is that I use Xcode as IDE.
When compiling in bash with g++ the program works fine.

Maybe some options in the Xcode build preferences are wrong.


Anyway, thanks for your helpful suggestions! :-)
Last edited on
bob10 (4)   Link to this post
The problem was that I compiled in Xcode for x64.
Switching to i386 solves the problem.

Registered users can post in this forum.