473,465 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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:ne*******@uk.thalesgroup.no.spam.please | Q: Is top-posting bad?
Please replace 'no.spam.please' with 'com' to reply directly.

-=-=-
.... APL is a write-only language. - Roy Keir
Jan 10 '06 #1
11 6242
FNX wrote:

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
Jan 10 '06 #2
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.

Jan 11 '06 #3
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

Jan 11 '06 #4

dc wrote:
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

Jan 11 '06 #5
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:ne*******@uk.thalesgroup.no.spam.please | Q: Is top-posting bad?
Please replace 'no.spam.please' with 'com' to reply directly.

-=-=-
.... Finish your mail packet! Children are offline in India.
Jan 11 '06 #6
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.

Jan 12 '06 #7
dc wrote:
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].

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jan 12 '06 #8
dc
Ok that means double quotes come in extended ascii range

Jan 12 '06 #9
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

Jan 12 '06 #10
dc
Micheil , I doubt
Following gave error on solaris 5.8
{
std::string aa="\u201c";
}

error: unknown escape sequence `\u'

Jan 12 '06 #11
dc wrote:
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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jan 12 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: kaalus | last post by:
Hi All, I just tried to use wide streams with C++. But something really strange is happening (my compiler is MSVC, wchar_t size is 2 bytes): std::wofstream s("a"); s << L'A'; s.close(); ...
9
by: wizofaus | last post by:
Is the any reason according to the standard that calling tellg() on an std::ifstream after a call to peek() could place the filebuf in an inconsistent state? I think it's a bug in the VC7...
3
by: Kirit Sælensminde | last post by:
>From thread http://groups.google.com/group/comp.lang.c++/browse_thread/thread/79d767efa42df516 "P.J. Plauger" <p...@dinkumware.comwrites: I'll take this at face value and I'll have to suppose...
3
by: mimi | last post by:
With a wofstream, we can input wstring into a file. But when i input some wide character of chinese characters into a file, some error occur. With the implementation of STLport 4.62, the wstring...
3
by: jraul | last post by:
Why does the following code create an empty file? I am using Windows XP. #include <iostream> #include <fstream> int main() { std::wofstream fout("data.txt");
6
by: Jeffrey Walton | last post by:
Hi All, I'm attempting to write a wstring to a file by way of wofstream. I'm getting compression on the stream (I presume it is UTF-8). How/where do I invoke an alternate constructotor so that...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.