how to get a formatted output right?

M

mwebel

Hi,
i want to get the adress of a pointer in a string. I did so by
outputting it to a ostringstream:
------------------------------
char* ptr;
//generate adress in hex format
std::eek:stringstream osstr,ossbyteSize ;
//This does not work anyway
//osstr.flags((std::ios_base::fmtflags)513);
//cout <<osstr.flags()<<endl;

osstr << &ptr;
std::string value = osstr.str();
cout << value<<endl;
---------------------------------


This works actually.. just the formatting is different when compiling
under Visual Studio and g++ Linux.
MSVC: 0022FFCC
Linux: 0x22ffcc
Id like to have a 8byte string containing the adress.. even if its lead
by some zeros.

Ok i said.. lets browse the net and set the formatting flags for both.
i got the flags of the windows version (513) and tried to set it for
both..(linux showed 4098).
Now linux also shows 513.. but the output is the same.

any ideas?
is this a maybe a compiler issue? could anybody reassure me that i did
the language part right?
 
R

Rolf Magnus

Hi,
i want to get the adress of a pointer in a string. I did so by
outputting it to a ostringstream:
------------------------------
char* ptr;
//generate adress in hex format
std::eek:stringstream osstr,ossbyteSize ;
//This does not work anyway
//osstr.flags((std::ios_base::fmtflags)513);

Where did you get that magic value of 513?
//cout <<osstr.flags()<<endl;

osstr << &ptr;
std::string value = osstr.str();
cout << value<<endl;
---------------------------------


This works actually.. just the formatting is different when compiling
under Visual Studio and g++ Linux.
MSVC: 0022FFCC
Linux: 0x22ffcc
Id like to have a 8byte string containing the adress.. even if its lead
by some zeros.

Well, the address is printed in a compiler-specific format for pointers. You
can do what you want by converting the pointer into a suitably sized
integer before putting it into the stream.
Ok i said.. lets browse the net and set the formatting flags for both.
i got the flags of the windows version (513) and tried to set it for
both..(linux showed 4098).
Now linux also shows 513.. but the output is the same.

The flags have names. Use them. The values are compiler-specific
 
G

Geo

Hi,
i want to get the adress of a pointer in a string. I did so by
outputting it to a ostringstream:
------------------------------
char* ptr;
//generate adress in hex format
std::eek:stringstream osstr,ossbyteSize ;
//This does not work anyway
//osstr.flags((std::ios_base::fmtflags)513);
//cout <<osstr.flags()<<endl;

osstr << &ptr;
std::string value = osstr.str();
cout << value<<endl;
---------------------------------


This works actually.. just the formatting is different when compiling
under Visual Studio and g++ Linux.
MSVC: 0022FFCC
Linux: 0x22ffcc
Id like to have a 8byte string containing the adress.. even if its lead
by some zeros.

Ok i said.. lets browse the net and set the formatting flags for both.
i got the flags of the windows version (513) and tried to set it for
both..(linux showed 4098).
Now linux also shows 513.. but the output is the same.

any ideas?
is this a maybe a compiler issue? could anybody reassure me that i did
the language part right?

If you want zero padded 8 digit hex values use this

cout << setfill('0') << setw(8) << hex << [value];

but it doesn't mean it will work for a pointer
 
M

mwebel

Rolf said:
Where did you get that magic value of 513?
by reading it from the windows version...

Anyway i now got it right by switching to sprintf().
The flags have names. Use them. The values are compiler-specific

Thanks, i didn't thought of that. i somehow assumed that values would
mean the same... but i now realize that alone different CPU
architectures might change this.

I actually first tried to solve the problem by using the examples shown
here:

http://www.cplusplus.com/ref/iostream/ios_base/setf.html
http://www.cplusplus.com/ref/iostream/ios_base/flags.html

but the first one wouldn't even compile in Linux(copy-pasted )! and
the second just didn't make any difference.
Both worked without problems on MSVC though.
I was using the same source file for both compilers...

Thanks for your responses!!
 
D

David Harmon

On 19 Dec 2006 04:06:19 -0800 in comp.lang.c++, (e-mail address removed) wrote,
Anyway i now got it right by switching to sprintf().

You got what you wanted by luck. sprintf() formatting of pointers is
implementation defined, just as iostream formatting. It's an open
question whether you had good luck or bad luck, since sooner or later
you will meet an implementation where sprintf() formatting differs and
you have to fix it again.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top