Can't output strings to std::wofstream (g++)

F

FNX

I'm stumbling around a little bit here trying to get a feel for
wide/Unicode functionality. I'm pretty up on C++ in general, streaming stuff
not so much.

If I run the following code, I get a zero length file. If I take out
*both* string outputs, I get '123abc' as expected. 'Last error' is always 0,
I get none of the debug error output.

Am I doing something wrong/obvisouly stupid? g++ 3.4.4 on Linux.


#include <iostream>
#include <fstream>
#include <errno.h>

int main()
{
std::string jobby = "Hello \u201Cwotsit\u201D\n";
std::wstring wjobby = L"Hello \u201Cwotsit\u201D\n";

std::wofstream file("jobby.out");
if (!file.good()) std::cerr << "Open error\n";

file << 123;
if (!file.good()) std::cerr << "Write error 1\n";

file << "abc";
if (!file.good()) std::cerr << "Write error 2\n";

file << jobby.c_str() << " normal char 10 is '" << jobby[10] << "'\n";
if (!file.good()) std::cerr << "Write error 3\n";

file << wjobby << L" wide char 10 is '" << wjobby[10] << L"'\n";
if (!file.good()) std::cerr << "Write error 4\n";

file.close();
std::cerr << "\nLast error " << errno << "\n";

return 0;
}


--
========================- http://www.thalesgroup.com/ -=======================
Neil Bird Principal Engineer | A: Yes
mailto:[email protected] | Q: Is top-posting bad?
Please replace 'no.spam.please' with 'com' to reply directly.

-=-=-
.... APL is a write-only language. - Roy Keir
 
T

Thomas Maier-Komor

FNX said:
I'm stumbling around a little bit here trying to get a feel for
wide/Unicode functionality. I'm pretty up on C++ in general, streaming
stuff not so much.

If I run the following code, I get a zero length file. If I take out
*both* string outputs, I get '123abc' as expected. 'Last error' is
always 0, I get none of the debug error output.

Am I doing something wrong/obvisouly stupid? g++ 3.4.4 on Linux.


#include <iostream>
#include <fstream>
#include <errno.h>

int main()
{
std::string jobby = "Hello \u201Cwotsit\u201D\n";
std::wstring wjobby = L"Hello \u201Cwotsit\u201D\n";

std::wofstream file("jobby.out");
if (!file.good()) std::cerr << "Open error\n";

file << 123;
if (!file.good()) std::cerr << "Write error 1\n";

file << "abc";
if (!file.good()) std::cerr << "Write error 2\n";

file << jobby.c_str() << " normal char 10 is '" << jobby[10] << "'\n";
if (!file.good()) std::cerr << "Write error 3\n";

file << wjobby << L" wide char 10 is '" << wjobby[10] << L"'\n";
if (!file.good()) std::cerr << "Write error 4\n";

file.close();
std::cerr << "\nLast error " << errno << "\n";

return 0;
}

Sun's CC says that \u201 is an Anachronism. I don't know what you are
doing here. But if I remove the u i.e. use \201, I get a valid output.

HTH,
Tom
 
F

FNX

'\u201C' is Unicode code-point 201C (opening quote). That's legit
and not to be confused with '\0201' + 'C'. Thus

"Hello \u201Cwotsit\u201D\n"
is
Hello "wotsit"

not
Hello ?Dwotsit?E

Dunno why Sun CC isn't having anything to do with it, and it's not
relevant (I think) to the failure to output.
 
D

dc

I think in C++ u cannot use \U to represent unicode characters.

Use \x201C.

Use unicode only in wstring as string is char* , so obviouly cannot
assign unicode value or use multibyte streams of \x201C in string
 
M

Michiel.Salters

dc said:
I think in C++ u cannot use \U to represent unicode characters.

You can, but it is 32-bits. \u takes 16 bits (4 hex digits) and sets
the upper
bits to all zero. Of course, very few people uses characters beyond
U+FFFF
 
F

FNX

Around about 10/01/06 14:28, FNX typed ...
If I run the following code, I get a zero length file. If I take out
*both* string outputs, I get '123abc' as expected. 'Last error' is
always 0, I get none of the debug error output.

Looks like all I was missing was a 'setlocale("");' at the beginning :-/

--
=======================- http://www.thalesgroup.com/ -=====================
Neil Bird Principal Engineer | A: Yes
mailto:[email protected] | Q: Is top-posting bad?
Please replace 'no.spam.please' with 'com' to reply directly.

-=-=-
.... Finish your mail packet! Children are offline in India.
 
D

dc

I really doubt if the problem could be solved by setlocale("") ( BTW
whats ur shell locale set to) as character \u201c (ie quotes) should
also be present in default locale code set( ISO8859-1 or any us ascii
compatible) ,,
When main() executes, setlocale to "C"( ie default locale) is done.
 
P

Pete Becker

dc said:
character \u201c (ie quotes) should
also be present in default locale code set( ISO8859-1 or any us ascii
compatible) ,,

'\u201c' is not a valid code point in the ASCII encoding, nor in any of
the 8859 encodings.

When talking about character representations it's important to
distinguish between the value held in a variable (the "code point") from
the character displayed on the screen (the "glyph"). ASCII code points
have values in the range [0,127]. 8859 extends that range to [0,255].
 
D

dc

just saw in windows default code page , hex value 0x93 refers to \U201c
unicode point.

But that should affect my previous stance on setlocale
 
D

dc

Micheil , I doubt
Following gave error on solaris 5.8
{
std::string aa="\u201c";
}

error: unknown escape sequence `\u'
 
P

Pete Becker

dc said:
Ok that means double quotes come in extended ascii range

Double quotes in ASCII (and all the 8859 encodings) are code point 0x22.
There may be other code points that represent the same glyph, but if you
write '"' in source code on a system that uses ASCII you'll get the
value 0x22.
 

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,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top