VC Unicode project read and write text file

Source: Internet
Author: User
Tags readfile

Reference article: http://blog.csdn.net/wuchen1004/archive/2010/08/03/5786537.aspx

Http://www.codeproject.com/KB/files/ConfigString.aspx

Because the Windows low-level processing string is handled in Unicode, the project for Unicode is now commonly used when developing software with VC.

In Unicode engineering, reading and writing text files can have a rather tangled problem:

In general, our new and edited text files are in ANSI format, while in the ANSI project when reading strings with very handy cstdiofile

Uses the CString type, which causes CStdioFile to read the ANSI string to the CSTRINGW type in the Unicode engineering environment.

A type mismatch error occurs even if you want to use ca2w to turn.

In this case, you have to use MultiByteToWideChar such APIs to convert, the actual encoding is always inconvenient. But even if you use this method,

It's easier than using ReadFile to read files, because after all, you don't have to deal with wrapping yourself.

For now, the easiest way to read an ANSI file in a Unicode engineering environment is to use fstream, which not only reads one line of text, but also makes it easy to read multiple digits or words in a row (see the example code that follows). There is an article on CodeProject that encapsulates two functions that read ANSI and Unicode strings, and he reads characters directly using FGETC and FGETWC, essentially the same way as ReadFile.

It is also noteworthy that in general, creating a new file is an ANSI type, and to create a Unicode file requires writing 0xFFFE to the first two bytes of the file.

There is a problem with using FStream to read Unicode files because of the two bytes at the beginning of the Unicode file, see the code example.

 int cctextfile_testdlg::cstdiofiletest (void) {CStdioFile file; CString Line; Read ANSI file file. Open (_t ("Test_ansi.txt"), Cfile::moderead); while (file. ReadString (line)) {OutputDebugString (line);//character garbled} file. Close (); Read Unicode file file. Open (_t ("Test_unicode.txt"), Cfile::moderead); while (file. ReadString (line)) {OutputDebugString (line);//garbled} file. Close (); Write the file, the resulting file is ANSI file files. Open (_t ("Test_stdiofile.txt"), Cfile::modecreate | Cfile::modewrite); File. WriteString (_t ("CStdioFile Test:")); Success file. WriteString (_t) ("Test Unicode.") /n ")); Write failed file. Close (); return 0; int cctextfile_testdlg::streamtest (void) {//Read ANSI file Std::ifstream isin Isin.open _t ("Test_ansi.txt"), Ios::in | i Os::binary); if (ISIN) {//reads a line of string; Getline (ISIN); OutputDebugString (ca2w (Line.c_str ())); Read strings and numbers, which is convenient for the stream to read the file string line2; int row; isin>>line2>>row; CString tmp = ca2w (Line2.c_str ()); Tmp. AppENdformat (_t ("%d"), row); OutputDebugString (TMP); Isin.close (); //Read Unicode file Std::wifstream Isinu; Isinu.open (_t ("Test_unicode.txt"), Ios::in | ios::binary); if (Isinu) {//reads one row wstring line; (Getline (Isinu, line)) {OutputDebugString (Line.c_str ());/garbled, read failed because Fffe opening} isinu.close (); //write an ANSI file, no problem ofstream aof; Aof.open (_t ("Test_ofstream.txt")); if (aof) {aof<< "stream ANSI test first row. "<<endl; aof<<123<< "" <<456<<endl; Aof.close (); //write file, get still is ANSI file, write English success wofstream of; Of.open (_t ("Test_wofstream.txt")); if (of) {of<<_t ("stream Unicode test first row.") ") <<endl; Failed to write Chinese characters of<<123<<l "" <<456<<endl; Of.close (); return 0; int cctextfile_testdlg::line2filetest (void) {FILE * fileansi = NULL; FILE * Fileuni = NULL; Read ANSI File Fileansi = fopen ("Test_ansi.txt", "RB"); if (Fileansi!= NULL) {string line; while (Linefromfile (Fileansi, line)) {OutputDebugString ( CA2W (Line.c_str ())); } fclose (Fileansi); } Fileuni = fopen ("Test_unicode.txt", "RB"); The Fileuni at the beginning of the IF (Fileuni!= NULL) {wstring line; while (Linefromfile (Ffee, line)) {//Unicode file is printed as '. ', no skipping OutputDebugString (Line.c_str ()); } fclose (Fileuni); //write an ANSI file Fileansi = fopen ("Test_line_ansi.txt", "WB"); if (Fileansi!= NULL) {linetofile (Fileansi, "ANSI file first line!"); Linetofile (Fileansi, "Line 2"); Fclose (Fileansi); //write file, get the ANSI file, but the string is unicode encoded Fileuni = fopen ("Test_line_unicode.txt", "WB"); if (Fileuni!= NULL) {linetofile (Fileuni, L "Unicode file first row!");//Success Linetofile (Fileuni, L "line 2"); fclose (file UNI); return 0; }

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.