Connecting Tech Pros Worldwide Forums | Help | Site Map

wofstream

jraul
Guest
 
Posts: n/a
#1: Jun 26 '07
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();
}
}


John Harrison
Guest
 
Posts: n/a
#2: Jun 26 '07

re: wofstream


jraul wrote:
Quote:
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
James Kanze
Guest
 
Posts: n/a
#3: Jun 26 '07

re: wofstream


On Jun 26, 6:42 am, jraul <jrauli...@yahoo.comwrote:
Quote:
Why does the following code create an empty file? I am using Windows
XP.
Quote:
#include <iostream>
#include <fstream>
Quote:
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.
Quote:
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:-).)
Quote:
fout.close();
}
}
--
James Kanze (GABI Software) email:james.kanze@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

Kevin Handy
Guest
 
Posts: n/a
#4: Jun 28 '07

re: wofstream


jraul wrote:
Quote:
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.
Quote:
{
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 =----
Closed Thread