473,396 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

wofstream

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");

if( fout )
{
fout << L"\u2620\u262D\u262F" << std::endl;

fout.close();
}
}

Jun 26 '07 #1
3 4889
jraul wrote:
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");

if( fout )
{
fout << L"\u2620\u262D\u262F" << std::endl;

fout.close();
}
}
Probably because the character \u2620 cannot be represented in your
current code page.

john
Jun 26 '07 #2
On Jun 26, 6:42 am, jraul <jrauli...@yahoo.comwrote:
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");
This creates a stream using the current default locale. Since
you haven't set the locale, you get the "C" locale.
if( fout )
{
fout << L"\u2620\u262D\u262F" << std::endl;
And there's no such character as "\u2620" in the "C" locale, so
conversion fails, which causes the output to fail, which means
that no further output can occur until you clear() the error
status.

Unless you are restricting your characters to US ASCII (and
if so, why bother with wofstream), you're main() should almost
always start:

std::locale::global( std::locale( "" ) ) ;
std::wcout.imbue( std::locale() ) ;
std::wcin.imbue( std::locale() ) ;
std::wcerr.imbue( std::locale() ) ;

Otherwise, it is probable that trying to output any character
not in the basic execution character set may fail. (Actually,
most implementations allow a few additional characters, such as
'$' or '@'. But this is not guaranteed. Of course, nothing you
do with the locales is really guaranteed either. If the user is
in a locale where there is no character "\u2620"---which would
be my case, for example---then you'll still end up with an
error. And to refer vaguely to another thread here: if the user
has selects a font with a different encoding than that of the
locale when he views the file, who knows what he'll get. But
the lines above make it the user's problem, and not yours:-).)
fout.close();
}
}
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 26 '07 #3
jraul wrote:
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");

if( fout )
What is fout at this point?

If the 'if' statement fails, don't expect the code
within to be executed.
{
fout << L"\u2620\u262D\u262F" << std::endl;

fout.close();
}
}
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jun 28 '07 #4

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(); ...
11
by: FNX | last post by:
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...
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...
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: Ioannis Vranos | last post by:
Regarding wifstreams/wofstreams, is it possible to create files (where applicable) with wchar_t names in standard C++, given that their equivalent constructors accept a const char * and not a const...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.