VC reads UTF-8 text files

Source: Internet
Author: User
Read the UTF-8 format text file remove the three bytes of the file header, first read the text data to the char array, then convert the multi-byte utf8 string into a wide character Unicode string, then convert the Unicode string to a char string or directly copy it to the cstring (both UTF-8 and char belong to multibyte char and cannot be directly converted to each other) bool readutf8stringfile (cstring path, cstring & Str) {cfile filer; If (! Filer. open (path, cfile: moderead | cfile: typebinary) {MessageBox (null, _ T ("file cannot be opened:") + path, _ T ("error"), mb_iconerror | mb_ OK); Return false;} byte head [3]; filer. read (Head, 3); If (! (Head [0] = 0xef & head [1] = 0xbb & head [2] = 0xbf) {filer. seektobegin ();} ulonglong filesize = filer. getlength (); char * Pcontent = (char *) calloc (filesize + 1, sizeof (char); filer. read (Pcontent, filesize); filer. close (); int n = multibytetowidechar (cp_utf8, 0, Pcontent, filesize + 1, null, 0); wchar_t * pwidechar = (wchar_t *) calloc (n + 1, sizeof (wchar_t); multibytetowidechar (cp_utf8, 0, Pcontent, filesize + 1, pwidechar, n); STR = cstring (pwidechar); free (Pcontent); free (pwidechar ); return true ;}

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.