473,396 Members | 2,111 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.

read unicode text file

Hi,

I'm just trying to print the contents of a notepad file I saved with
unicode encoding on my win xp machine. I keep getting strange
characters printed out though. Why doesn't this work?

std::wifstream wifile("C:\\unicode.txt");

if (wifile.is_open()) {
wchar_t wszBuffer[MAX_PATH];

while (wifile.getline(wszBuffer, MAX_PATH)) {
AfxMessageBox(wszBuffer);
}
}

My unicode saved file just has the contents "hello" in it, but my
message box prints some crazy stuff.

Thanks

Jul 24 '06 #1
1 4039
markww wrote:
Hi,

I'm just trying to print the contents of a notepad file I saved with
unicode encoding on my win xp machine.
There are at least three ways to do that, and that's just counting
Notepad.
There are more ways to serialize Unicode.
I keep getting strange
characters printed out though. Why doesn't this work?

std::wifstream wifile("C:\\unicode.txt");

if (wifile.is_open()) {
wchar_t wszBuffer[MAX_PATH];

while (wifile.getline(wszBuffer, MAX_PATH)) {
AfxMessageBox(wszBuffer);
}
}
Basically, because wifstream doesn't understand Unicode. What you need
is "a" unicode codecvt. Since there are multiple ways to serialize
Unicode,
which codecvt you need depends the encoding. And unfortunately, the
standard doesn't require one Unicode codecvt. You might need to buy
one.
The easier solution, in many cases: Restrict yourself to UTF-8; read
entire
lines at a time, and convert them yourself.

HTH,
Michiel Salters

Jul 24 '06 #2

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

Similar topics

4
by: Achim Domma | last post by:
Hi, I read some text from a utf-8 encoded text file like this: text = codecs.open('example.txt','r','utf8').read() If I pass this text to a COM object, I can see that there is still the BOM...
17
by: Guyon Morée | last post by:
what is the difference? if I open a text file in binary (rb) mode, it doesn't matter... the read() output is the same.
3
by: hunterb | last post by:
I have a file which has no BOM and contains mostly single byte chars. There are numerous double byte chars (Japanese) which appear throughout. I need to take the resulting Unicode and store it in a...
2
by: hezhenjie | last post by:
Hi, all: I just need to parse a unicode file, and assume to get data one line by one line. I use _wfopen(), fgetws(), wcslen(), wcsstr(), making it work normally on Windows platform. However,...
4
by: Kueishiong Tu | last post by:
I have a text file with wide characters. I use the following C++ code to read them in. However the wide characters are not read in properly. What is wrong? String* path = "C:\\Documents and...
10
by: Tibby | last post by:
I need to read/write not only text files, but binary as well. It seems like on binary files, it doesn't right the last 10% of the file. -- Thanks --- Outgoing mail is certified Virus...
0
by: Ahmed A. | last post by:
This will be very helpfull for many! Using RichTextBox Read/Write Unicode File http://www.microsoft.com/indonesia/msdn/wnf_RichTextBox.as p Private Function ReadFile(ByVal myfile As String)...
2
by: starffly | last post by:
I want to read a xml file in Unicode, UTF-8 or a native encoding into a wchar_t type string, so i write a routine as follows, however, sometimes a Unicode file including Chinese character cannot...
14
by: Zoro | last post by:
My task is to read html files from disk and save them onto SQL Server database field. I have created an nvarchar(max) field to hold them. The problem is that some characters, particularly html...
0
by: Steve Holden | last post by:
ganesh gajre wrote: You are getting too ambitious. Text files don't have any font information associated with them. Not only that, but the encoding of Unicode character data is independent of...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.