Read and Write Unicode files in the ANSI environment of VC Programming

Source: Internet
Author: User
Read and Write Unicode files in the ANSI environment of VC Programming

I did not notice that the differences in file encoding will cause so many problems. I have searched a lot of information before I started, and I have added many of my predecessors to my blog. I would like to pay tribute to them here!
I will not talk about the principle of ANSI and Unicode encoding here. I will mainly talk about how to read and write!
First, determine the encoding environment that your project uses. The default value is ANSI. The differences between reading and writing files of different character sets are also large. I only do this in the ANSI environment, the next step is to explore how to read and write in the Unicode environment! (I did not understand this. I read a lot of code and found that my experiment was wrong ).
In the ANSI character set, cstring and so on are all single-byte versions, so be sure to pay attention. The Unicode files to be read are dual-byte files, which need to be converted here. Of course, in the ANSI character set, you should open the Unicode file in binary mode and determine whether it is a line break, convert to ANSI encoding. When writing Unicode, convert the character into unicode encoding before writing it, and add the Unicode file ID before writing the file.
Below is the read
Cfile mfile (unicodefilepath, cfile: moderead );
Byte head [2];
Mfile. Read (Head, 2 );
If (head [0] = 0xff & head [1] = 0xfe) | (head [0] = 0xfe & head [1] = 0xff ))
{
// Afxmessagebox (_ T ("file is Unicode! "));
Isunicode = true;
}
If (isunicode) mfile. Seek (2, cfile: Begin); // 0 xfffe
Wchar_t wch;
Wchar_t wstr [300];

Cstring strvalue;
Hile (mfile. Read (char *) & wch, 2)> 0)
{
If (wch = 0x000d) // by line
{
// Chang to ANSI
Int nlen = I;
Char * Buf = new char [2 * nlen];
Widechartomultibyte (cp_acp, 0, wstr, nlen, Buf, 2 * nlen, null, null );
Buf [2 * nLen-1] = 0; // some assertion failed, this is more important, a small problem can be tossing people ah
Strvalue = Buf;
Mfile. Seek (2, cfile: Current); // skip the starting line symbol
I = 0;
}
Else
{
Wstr [I ++] = wch;
}
}
// Below is the write
Cstdiofile transfile;
Transfile. Open (strunicodesavepath, cfile: modecreate | cfile: modewrite | cfile: typebinary );
Word wsignature = 0 xfeff;
Transfile. Write (& wsignature, 2); // Unicode file symbol
Char * pszansi = new tchar [strvalue. getlength () + 1];
_ Tcscpy (pszansi, strvalue );
Wchar * szwbuffer = new wchar [strvalue. getlength () + 1];
Multibytetowidechar (cp_acp, 0, pszansi,-1, szwbuffer, strvalue. getlength () + 1 );
// Write to files
Transfile. Write (szwbuffer, lstrlenw (szwbuffer) * sizeof (wchar ));
Of course, you can set your project to a Unicode Character Set. Reading ANSI files in a unicode project is also an annoying thing. When reading files into cstring, each single-byte ANSI is converted to a dual-byte and needs to be processed by myself. I will explore and record it later.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.