what is "wofstream" useful for?!!
-
wofstream
is a type definition of thebasic_ofstream
class with thewchar_t
data type. And so it supports wide characters. The 2 statements that you have written are definitely possible.«_Superman_»
I love work. It gives me something to do between weekends.no it's not! it just creates an empty file. I've tried it 10's times.
-
no it's not! it just creates an empty file. I've tried it 10's times.
Have you tried writing the BOM (Byte Order Mark) header in the beginning of the file?
«_Superman_»
I love work. It gives me something to do between weekends. -
Have you tried writing the BOM (Byte Order Mark) header in the beginning of the file?
«_Superman_»
I love work. It gives me something to do between weekends.yes I have. to be more clear, this damn class writes ASCII characters quiet well. the problem I have is about non-English characters.
wofstream of (L"FileName.txt"); of << L"English"; // this works of << L"فارسی"; // this does NOT work of.close();
-
as I've heard, the class wofstream narrows every thing when it writes to a file. that is it converts every wide character to a single byte one and then writes it to the file. in this way you can't have such a thing as:
wofstream out(L"foo.txt"); out << L"unicode string";
so there is one question: what's this WIDE class useful for?!! how you name it WIDE version of "ofstream" while it can't be used with wide characters? and one more thing: why it takes a parameter of type char* as the file name? it's a wide class any way :)It can be used with wide characters, no problem at all. The C++ 98 standard library assumes that all filenames are narrow characters so the first line you've presented won't compile on a standard conforming compiler - however some compilers provide the second form as an extension to the library. What's particularly brain-dead about the constructors are the fact that they take C style strings and not std::basic_strings, but that's another story. The line you've presented works perfectly - it handles wide characters pretty well in my experience. What it stores them in the file doesn't really matter (as far as the standard library is concerned) provided you can read them back in the same form. Cheers, Ash
-
yes I have. to be more clear, this damn class writes ASCII characters quiet well. the problem I have is about non-English characters.
wofstream of (L"FileName.txt"); of << L"English"; // this works of << L"فارسی"; // this does NOT work of.close();
-
Have you tried calling std::basic_fstream::imbue with the locale of the text you want to write? Have a read of Kreft and Langer "Standard C++ IOStreams and Locales" for as many details as you can take and then a few more. Cheers, Ash
no I have not, and thank you for your clue. I'll read it as soon as return back my home. and one more thing, could you please show me some more resources on these new things (STL::locals). I've no previous experience on that.
-
It can be used with wide characters, no problem at all. The C++ 98 standard library assumes that all filenames are narrow characters so the first line you've presented won't compile on a standard conforming compiler - however some compilers provide the second form as an extension to the library. What's particularly brain-dead about the constructors are the fact that they take C style strings and not std::basic_strings, but that's another story. The line you've presented works perfectly - it handles wide characters pretty well in my experience. What it stores them in the file doesn't really matter (as far as the standard library is concerned) provided you can read them back in the same form. Cheers, Ash
no, don't get it wrong. in my case, when I have such a line as
of << L"فارسی";
It does not write ANY THING AT ALL into the file. strange, ha? -
no I have not, and thank you for your clue. I'll read it as soon as return back my home. and one more thing, could you please show me some more resources on these new things (STL::locals). I've no previous experience on that.
Sorry, I don't know a good online resource for locales which is why I recommended Kreft and Langer's book. However, Emilio (who posts on here a fair bit) wrote an article about plugging a UTF-8[^] codecvt facet for locales which may give you enough information to get started with part of locales. You can try modifying his code to read and write UTF-16/UCS2/UCS4 depending on your particular needs. Cheers, Ash
-
Sorry, I don't know a good online resource for locales which is why I recommended Kreft and Langer's book. However, Emilio (who posts on here a fair bit) wrote an article about plugging a UTF-8[^] codecvt facet for locales which may give you enough information to get started with part of locales. You can try modifying his code to read and write UTF-16/UCS2/UCS4 depending on your particular needs. Cheers, Ash
thank you so much. :rose: for you :)
-
no, don't get it wrong. in my case, when I have such a line as
of << L"فارسی";
It does not write ANY THING AT ALL into the file. strange, ha?