指定存储文件的编码格式(上)
2015-01-25 20:11
393 查看
终于搞完了,内容稍微有点多分为两篇把。
《指定存储文件的编码格式(上)》
《指定存储文件的编码格式(下)》
本篇为上篇。
主流的文件编码包括:UTF8\UTF8-WITHOUT-BOM\UTF16LE\UTF16BE\ANSI等。
中文的windows操作系统默认使用就是ANSI编码。
各种编码的主要规则大家可以去wiki网站、unicode.org网站等查看。
本文的上篇和下篇都采用windows函数WideCharToMultiByte和MultiByteToWideChar为基础进行编写的。
本文的上篇和下篇主要完成从指定文件A中读取数据,输出到指定编码的文件B中。
之所以分成上下两篇主要是下篇是对上篇的改进和优化。
本篇源码主要片段:
*注:由于较长建议大家放到编译器里面阅读。具体编码实例放到这里。
《指定存储文件的编码格式(上)》
《指定存储文件的编码格式(下)》
本篇为上篇。
主流的文件编码包括:UTF8\UTF8-WITHOUT-BOM\UTF16LE\UTF16BE\ANSI等。
中文的windows操作系统默认使用就是ANSI编码。
各种编码的主要规则大家可以去wiki网站、unicode.org网站等查看。
本文的上篇和下篇都采用windows函数WideCharToMultiByte和MultiByteToWideChar为基础进行编写的。
本文的上篇和下篇主要完成从指定文件A中读取数据,输出到指定编码的文件B中。
之所以分成上下两篇主要是下篇是对上篇的改进和优化。
本篇源码主要片段:
// test__OutputUtf8File.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <windows.h> #include <string> #include <iostream> #define UTF8_SIGN 3 #define UTF16_SIGN 2 //************************************ // Method: Utf16leFileToUtf8File // FullName: Utf16leFileToUtf8File // Access: public // Returns: BOOL // Qualifier:将lpUtf16leFile文件内容写入到lpUtf8File文件中 // Parameter: CONST LPTSTR lpUtf16leFile:输入文件utf16le编码 // Parameter: CONST LPTSTR lpUtf8File:输出文件为utf8-with-BOM编码 // *注:lpUtf16leFile文件只读;lpUtf8File文件总是创建或覆盖 //************************************ BOOL Utf16leFileToUtf8File(CONST LPTSTR lpUtf16leFile, CONST LPTSTR lpUtf8File) { HANDLE hUtf16leFile = NULL; HANDLE hUtf8File = NULL; //create file hUtf16leFile = ::CreateFile(lpUtf16leFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hUtf16leFile) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); return FALSE; } //read UTF16LE encode file content LPWSTR lpReadContentByUTF16 = NULL; DWORD cbReadContentByUTF16 = 0; DWORD cbPreReadContentByUTF16 = 0; DWORD cchReadContentByUTF16 = 0; cbReadContentByUTF16 = SetFilePointer(hUtf16leFile, 0, NULL, FILE_END); if (INVALID_SET_FILE_POINTER == cbReadContentByUTF16) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); ::CloseHandle(hUtf16leFile); return FALSE; } lpReadContentByUTF16 = (WCHAR *)malloc(cbReadContentByUTF16); if (NULL == lpReadContentByUTF16) { printf_s("malloc error\n"); ::CloseHandle(hUtf16leFile); return FALSE; } ZeroMemory(lpReadContentByUTF16, cbReadContentByUTF16); SetFilePointer(hUtf16leFile, 0, NULL, FILE_BEGIN); if (FALSE == ReadFile(hUtf16leFile, lpReadContentByUTF16, cbReadContentByUTF16, &cbPreReadContentByUTF16, NULL)) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); free(lpReadContentByUTF16); ::CloseHandle(hUtf16leFile); return FALSE; } cchReadContentByUTF16 = ((cbReadContentByUTF16 % sizeof(WCHAR)) != 0 ? (cbReadContentByUTF16 / sizeof(WCHAR) + 1) : (cbReadContentByUTF16 / sizeof(WCHAR))); //transform encode LPSTR lpWriteContentByUTF8 = NULL; DWORD cchWriteContentByUTF8 = 0; DWORD cbWriteContentByUTF8 = 0; DWORD cbPreWriteContentByUTF8 = 0; cbWriteContentByUTF8 = ((cbReadContentByUTF16 % sizeof(WCHAR)) != 0 ? (cbReadContentByUTF16 + sizeof(WCHAR)) : (cbReadContentByUTF16)); cchWriteContentByUTF8 = cchReadContentByUTF16; lpWriteContentByUTF8 = (CHAR *)malloc(cbWriteContentByUTF8); if (NULL == lpWriteContentByUTF8) { printf_s("malloc error\n"); free(lpReadContentByUTF16); ::CloseHandle(hUtf16leFile); return FALSE; } ZeroMemory(lpWriteContentByUTF8, cbWriteContentByUTF8); if (0 == WideCharToMultiByte(CP_UTF8, 0, lpReadContentByUTF16, cchReadContentByUTF16, lpWriteContentByUTF8, cbWriteContentByUTF8, NULL, NULL)) { printf_s("transform error\n"); free(lpReadContentByUTF16); free(lpWriteContentByUTF8); ::CloseHandle(hUtf16leFile); return FALSE; } //write UTF8 encode file content hUtf8File = ::CreateFile(lpUtf8File, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hUtf8File) { printf("Terminal failure: Unable to write to file.\n"); free(lpReadContentByUTF16); free(lpWriteContentByUTF8); ::CloseHandle(hUtf16leFile); return FALSE; } for (int i = 0; i != cbWriteContentByUTF8; ++i) { if (TEXT('\0')==lpWriteContentByUTF8[i]) { cchWriteContentByUTF8 = i; //lpWriteContentByUTF8[i] = 0;//设置结束符,但是utf8好像不用把 break; } } SetFilePointer(hUtf8File, 0, NULL, FILE_BEGIN); if (FALSE == WriteFile(hUtf8File, lpWriteContentByUTF8, cchWriteContentByUTF8, &cbPreWriteContentByUTF8, NULL)) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); free(lpReadContentByUTF16); free(lpWriteContentByUTF8); ::CloseHandle(hUtf16leFile); return FALSE; } //release resource free(lpReadContentByUTF16); free(lpWriteContentByUTF8); ::CloseHandle(hUtf16leFile); ::CloseHandle(hUtf8File); return TRUE; } //************************************ // Method: Utf16leFileToUtf8NoBOMFile // FullName: Utf16leFileToUtf8NoBOMFile // Access: public // Returns: BOOL // Qualifier:将lpUtf16leFile文件内容写入到lpUtf8NoBOMFile文件中 // Parameter: CONST LPTSTR lpUtf16leFile:输入文件utf16le编码 // Parameter: CONST LPTSTR lpUtf8NoBOMFile:输出文件为utf8-without-BOM编码 // *注:lpUtf16leFile文件只读;lpUtf8NoBOMFile文件总是创建或覆盖 //************************************ BOOL Utf16leFileToUtf8NoBOMFile(CONST LPTSTR lpUtf16leFile, CONST LPTSTR lpUtf8NoBOMFile) { HANDLE hUtf16leFile = NULL; HANDLE hUtf8NoBOMFile = NULL; //create UTF16LE file hUtf16leFile = ::CreateFile(lpUtf16leFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hUtf16leFile) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); return FALSE; } //read UTF16LE encode file content LPWSTR lpReadContentByUTF16 = NULL; DWORD cbReadContentByUTF16 = 0; DWORD cbPreReadContentByUTF16 = 0; DWORD cchReadContentByUTF16 = 0; cbReadContentByUTF16 = SetFilePointer(hUtf16leFile, 0, NULL, FILE_END); if (INVALID_SET_FILE_POINTER == cbReadContentByUTF16) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); ::CloseHandle(hUtf16leFile); return FALSE; } lpReadContentByUTF16 = (WCHAR *)malloc(cbReadContentByUTF16); if (NULL == lpReadContentByUTF16) { printf_s("malloc error\n"); ::CloseHandle(hUtf16leFile); return FALSE; } ZeroMemory(lpReadContentByUTF16, cbReadContentByUTF16); SetFilePointer(hUtf16leFile, 0, NULL, FILE_BEGIN); if (FALSE == ReadFile(hUtf16leFile, lpReadContentByUTF16, cbReadContentByUTF16, &cbPreReadContentByUTF16, NULL)) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); free(lpReadContentByUTF16); ::CloseHandle(hUtf16leFile); return FALSE; } cchReadContentByUTF16 = ((cbReadContentByUTF16 % sizeof(WCHAR)) != 0 ? (cbReadContentByUTF16 / sizeof(WCHAR) + 1) : (cbReadContentByUTF16 / sizeof(WCHAR))); //transform encode LPSTR lpWriteContentByUTF8 = NULL; DWORD cchWriteContentByUTF8 = 0; DWORD cbWriteContentByUTF8 = 0; DWORD cbPreWriteContentByUTF8 = 0; cbWriteContentByUTF8 = ((cbReadContentByUTF16 % sizeof(WCHAR)) != 0 ? (cbReadContentByUTF16 + sizeof(WCHAR)) : (cbReadContentByUTF16)); cchWriteContentByUTF8 = cchReadContentByUTF16; lpWriteContentByUTF8 = (CHAR *)malloc(cbWriteContentByUTF8); if (NULL == lpWriteContentByUTF8) { printf_s("malloc error\n"); free(lpReadContentByUTF16); ::CloseHandle(hUtf16leFile); return FALSE; } ZeroMemory(lpWriteContentByUTF8, cbWriteContentByUTF8); if (0 == WideCharToMultiByte(CP_UTF8, 0, lpReadContentByUTF16, cchReadContentByUTF16, lpWriteContentByUTF8, cbWriteContentByUTF8, NULL, NULL)) { printf_s("transform error\n"); free(lpReadContentByUTF16); free(lpWriteContentByUTF8); ::CloseHandle(hUtf16leFile); return FALSE; } //write UTF8NoBOM encode file content LPSTR lpWriteContentByUTF8NOBOM = NULL; DWORD cbWriteContentByUTF8NOBOM = 0; hUtf8NoBOMFile = ::CreateFile(lpUtf8NoBOMFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hUtf8NoBOMFile) { printf("Terminal failure: Unable to write to file.\n"); free(lpReadContentByUTF16); free(lpWriteContentByUTF8); ::CloseHandle(hUtf16leFile); return FALSE; } for (int i = 0; i != cbWriteContentByUTF8; ++i) { if (TEXT('\0') == lpWriteContentByUTF8[i]) { cbWriteContentByUTF8NOBOM = i - UTF8_SIGN; break; } } lpWriteContentByUTF8NOBOM = (CHAR *)malloc(cbWriteContentByUTF8NOBOM); if (NULL == lpWriteContentByUTF8NOBOM) { printf_s("malloc error\n"); free(lpReadContentByUTF16); free(lpWriteContentByUTF8); ::CloseHandle(hUtf16leFile); return FALSE; } ZeroMemory(lpWriteContentByUTF8NOBOM, cbWriteContentByUTF8NOBOM); CopyMemory(lpWriteContentByUTF8NOBOM, lpWriteContentByUTF8 + UTF8_SIGN, cbWriteContentByUTF8NOBOM); SetFilePointer(hUtf8NoBOMFile, 0, NULL, FILE_BEGIN); if (FALSE == WriteFile(hUtf8NoBOMFile, lpWriteContentByUTF8NOBOM, cbWriteContentByUTF8NOBOM, &cbPreWriteContentByUTF8, NULL)) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); free(lpReadContentByUTF16); free(lpWriteContentByUTF8); free(lpWriteContentByUTF8NOBOM); ::CloseHandle(hUtf16leFile); return FALSE; } //release resource free(lpReadContentByUTF16); free(lpWriteContentByUTF8); free(lpWriteContentByUTF8NOBOM); ::CloseHandle(hUtf16leFile); ::CloseHandle(hUtf8NoBOMFile); return TRUE; } //************************************ // Method: Utf16leFileToUtf8NoBOMFile2 // FullName: Utf16leFileToUtf8NoBOMFile2 // Access: public // Returns: BOOL // Qualifier: // Parameter: CONST LPTSTR lpUtf16leFile:输入文件utf16le编码 // Parameter: CONST LPTSTR lpUtf8NoBOMFile:输出文件为utf8-without-BOM编码 // *注:lpUtf16leFile文件只读;lpUtf8NoBOMFile文件总是创建或覆盖 //************************************ BOOL Utf16leFileToUtf8NoBOMFile2(CONST LPTSTR lpUtf16leFile, CONST LPTSTR lpUtf8NoBOMFile) { HANDLE hUtf16leFile = NULL; HANDLE hUtf8NoBOMFile = NULL; //create UTF16LE file hUtf16leFile = ::CreateFile(lpUtf16leFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hUtf16leFile) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); return FALSE; } //read UTF16LE encode file content LPWSTR lpReadContentByUTF16 = NULL; DWORD cbReadContentByUTF16 = 0; DWORD cbPreReadContentByUTF16 = 0; DWORD cchReadContentByUTF16 = 0; cbReadContentByUTF16 = SetFilePointer(hUtf16leFile, 0, NULL, FILE_END); if (INVALID_SET_FILE_POINTER == cbReadContentByUTF16) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); ::CloseHandle(hUtf16leFile); return FALSE; } lpReadContentByUTF16 = (WCHAR *)malloc(cbReadContentByUTF16); if (NULL == lpReadContentByUTF16) { printf_s("malloc error\n"); ::CloseHandle(hUtf16leFile); return FALSE; } ZeroMemory(lpReadContentByUTF16, cbReadContentByUTF16); SetFilePointer(hUtf16leFile, 0, NULL, FILE_BEGIN); if (FALSE == ReadFile(hUtf16leFile, lpReadContentByUTF16, cbReadContentByUTF16, &cbPreReadContentByUTF16, NULL)) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); free(lpReadContentByUTF16); ::CloseHandle(hUtf16leFile); return FALSE; } cchReadContentByUTF16 = ((cbReadContentByUTF16 % sizeof(WCHAR)) != 0 ? (cbReadContentByUTF16 / sizeof(WCHAR) + 1) : (cbReadContentByUTF16 / sizeof(WCHAR))); //transform encode LPSTR lpWriteContentByUTF8 = NULL; DWORD cchWriteContentByUTF8 = 0; DWORD cbWriteContentByUTF8 = 0; DWORD cbPreWriteContentByUTF8 = 0; cbWriteContentByUTF8 = ((cbReadContentByUTF16 % sizeof(WCHAR)) != 0 ? (cbReadContentByUTF16 + sizeof(WCHAR)) : (cbReadContentByUTF16)); cchWriteContentByUTF8 = cchReadContentByUTF16; lpWriteContentByUTF8 = (CHAR *)malloc(cbWriteContentByUTF8); if (NULL == lpWriteContentByUTF8) { printf_s("malloc error\n"); free(lpReadContentByUTF16); ::CloseHandle(hUtf16leFile); return FALSE; } ZeroMemory(lpWriteContentByUTF8, cbWriteContentByUTF8); if (0 == WideCharToMultiByte(CP_UTF8, 0, lpReadContentByUTF16, cchReadContentByUTF16, lpWriteContentByUTF8, cbWriteContentByUTF8, NULL, NULL)) { printf_s("transform error\n"); free(lpReadContentByUTF16); free(lpWriteContentByUTF8); ::CloseHandle(hUtf16leFile); return FALSE; } //write UTF8NOBOM file content hUtf8NoBOMFile = ::CreateFile(lpUtf8NoBOMFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hUtf8NoBOMFile) { printf("Terminal failure: Unable to write to file.\n"); free(lpReadContentByUTF16); free(lpWriteContentByUTF8); ::CloseHandle(hUtf16leFile); return FALSE; } for (int i = 0; i != cbWriteContentByUTF8; ++i) { if (TEXT('\0') == lpWriteContentByUTF8[i]) { cchWriteContentByUTF8 = i; break; } } SetFilePointer(hUtf8NoBOMFile, 0, NULL, FILE_BEGIN); if (FALSE == WriteFile(hUtf8NoBOMFile, lpWriteContentByUTF8 + UTF8_SIGN, cchWriteContentByUTF8 - UTF8_SIGN, &cbPreWriteContentByUTF8, NULL)) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); free(lpReadContentByUTF16); free(lpWriteContentByUTF8); ::CloseHandle(hUtf16leFile); ::CloseHandle(hUtf8NoBOMFile); return FALSE; } //release resource free(lpReadContentByUTF16); free(lpWriteContentByUTF8); ::CloseHandle(hUtf16leFile); ::CloseHandle(hUtf8NoBOMFile); return TRUE; } //************************************ // Method: Utf8FileToUtf16leFile // FullName: Utf8FileToUtf16leFile // Access: public // Returns: BOOL // Qualifier:将utf8编码格式文件转换为utf16le编码格式文件 // Parameter: CONST LPTSTR lpUtf8File:utf8编码格式文件 // Parameter: CONST LPTSTR lpUtf16leFile:utf16le编码格式文件 //************************************ BOOL Utf8FileToUtf16leFile(CONST LPTSTR lpUtf8File, CONST LPTSTR lpUtf16leFile) { HANDLE hUtf16leFile = NULL; HANDLE hUtf8File = NULL; //create UTF8 file hUtf8File = ::CreateFile(lpUtf8File, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hUtf8File) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); return FALSE; } //read UTF8 encode file content LPSTR lpReadContentByUTF8 = NULL; DWORD cbReadContentByUTF8 = 0; DWORD cbPreReadContentByUTF8 = 0; DWORD cchReadContentByUTF8 = 0; cbReadContentByUTF8 = SetFilePointer(hUtf8File, 0, NULL, FILE_END); if (INVALID_SET_FILE_POINTER == cbReadContentByUTF8) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); ::CloseHandle(hUtf8File); return FALSE; } lpReadContentByUTF8 = (CHAR *)malloc(cbReadContentByUTF8); if (NULL == lpReadContentByUTF8) { printf_s("malloc error\n"); ::CloseHandle(hUtf8File); return FALSE; } ZeroMemory(lpReadContentByUTF8, cbReadContentByUTF8); SetFilePointer(hUtf8File, 0, NULL, FILE_BEGIN); if (FALSE == ReadFile(hUtf8File, lpReadContentByUTF8, cbReadContentByUTF8, &cbPreReadContentByUTF8, NULL)) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); free(lpReadContentByUTF8); ::CloseHandle(hUtf8File); return FALSE; } if (sizeof(CHAR) != sizeof(BYTE)) { free(lpReadContentByUTF8); ::CloseHandle(hUtf8File); return FALSE; } cchReadContentByUTF8 = cbReadContentByUTF8; //transform encode LPWSTR lpWriteContentByUTF16 = NULL; DWORD cchWriteContentByUTF16 = 0; DWORD cbWriteContentByUTF16 = 0; DWORD cbPreWriteContentByUTF16 = 0; cbWriteContentByUTF16 = cchReadContentByUTF8 * 2; cchWriteContentByUTF16 = cchReadContentByUTF8; lpWriteContentByUTF16 = (WCHAR *)malloc(cbWriteContentByUTF16); if (NULL == lpWriteContentByUTF16) { printf_s("malloc error\n"); free(lpReadContentByUTF8); ::CloseHandle(hUtf8File); return FALSE; } ZeroMemory(lpWriteContentByUTF16, cbWriteContentByUTF16); if (0 == MultiByteToWideChar(CP_UTF8, 0, lpReadContentByUTF8, cbReadContentByUTF8, lpWriteContentByUTF16, cchWriteContentByUTF16)) { printf_s("transform error\n"); free(lpReadContentByUTF8); free(lpWriteContentByUTF16); ::CloseHandle(hUtf8File); return FALSE; } //write UTF16LE encode file content hUtf16leFile = ::CreateFile(lpUtf16leFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hUtf16leFile) { printf("Terminal failure: Unable to write to file.\n"); free(lpReadContentByUTF8); free(lpWriteContentByUTF16); ::CloseHandle(hUtf8File); return FALSE; } for (int i = 0; i != cbWriteContentByUTF16 -1; ++i) { if (0 == lpWriteContentByUTF16[i] && 0 == lpWriteContentByUTF16[i+1]) { cbWriteContentByUTF16 = i; break; } } ////////////////////////////////////////////////////////////////////////// //std::wstring wstrText = L"output to utf8 with BOM.\r\n输出到utf8带BOM的文件中。\r\nengli sh"; //int lgText = wstrText.length(); //int lgText2 = wstrText.size(); //std::cout << lgText << "" << lgText2 << std::endl; ////////////////////////////////////////////////////////////////////////// SetFilePointer(hUtf16leFile, 0, NULL, FILE_BEGIN); if (FALSE == WriteFile(hUtf16leFile, lpWriteContentByUTF16, cbWriteContentByUTF16 * sizeof(WCHAR), &cbPreWriteContentByUTF16, NULL)) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); free(lpReadContentByUTF8); free(lpWriteContentByUTF16); ::CloseHandle(hUtf16leFile); ::CloseHandle(hUtf8File); return FALSE; } //release resource free(lpReadContentByUTF8); free(lpWriteContentByUTF16); ::CloseHandle(hUtf16leFile); ::CloseHandle(hUtf8File); return TRUE; } BOOL Utf8NoBOMFileToUtf16leFile(CONST LPTSTR lpUtfNoBOM8File, CONST LPTSTR lpUtf16leFile) { return Utf8FileToUtf16leFile(lpUtfNoBOM8File, lpUtf16leFile); //HANDLE hUtf16leFile = NULL; //HANDLE hUtf8File = NULL; ////create UTF8 file //hUtf8File = ::CreateFile(lpUtf8File, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); //if (INVALID_HANDLE_VALUE == hUtf8File) //{ // int errLogNumber = GetLastError(); // printf_s("error number:%d\n", errLogNumber); // return FALSE; //} ////read UTF8 encode file content //LPSTR lpReadContentByUTF8 = NULL; //DWORD cbReadContentByUTF8 = 0; //DWORD cbPreReadContentByUTF8 = 0; //DWORD cchReadContentByUTF8 = 0; //cbReadContentByUTF8 = SetFilePointer(hUtf8File, 0, NULL, FILE_END); //if (INVALID_SET_FILE_POINTER == cbReadContentByUTF8) //{ // int errLogNumber = GetLastError(); // printf_s("error number:%d\n", errLogNumber); // ::CloseHandle(hUtf8File); // return FALSE; //} //lpReadContentByUTF8 = (CHAR *)malloc(cbReadContentByUTF8); //if (NULL == lpReadContentByUTF8) //{ // printf_s("malloc error\n"); // ::CloseHandle(hUtf8File); // return FALSE; //} //ZeroMemory(lpReadContentByUTF8, cbReadContentByUTF8); //SetFilePointer(hUtf8File, 0, NULL, FILE_BEGIN); //if (FALSE == ReadFile(hUtf8File, lpReadContentByUTF8, cbReadContentByUTF8, &cbPreReadContentByUTF8, NULL)) //{ // int errLogNumber = GetLastError(); // printf_s("error number:%d\n", errLogNumber); // free(lpReadContentByUTF8); // ::CloseHandle(hUtf8File); // return FALSE; //} //if (sizeof(CHAR) != sizeof(BYTE)) //{ // free(lpReadContentByUTF8); // ::CloseHandle(hUtf8File); // return FALSE; //} //cchReadContentByUTF8 = cbReadContentByUTF8; ////transform encode //LPWSTR lpWriteContentByUTF16 = NULL; //DWORD cchWriteContentByUTF16 = 0; //DWORD cbWriteContentByUTF16 = 0; //DWORD cbPreWriteContentByUTF16 = 0; //cbWriteContentByUTF16 = cchReadContentByUTF8 * 2; //cchWriteContentByUTF16 = cchReadContentByUTF8; //lpWriteContentByUTF16 = (WCHAR *)malloc(cbWriteContentByUTF16); //if (NULL == lpWriteContentByUTF16) //{ // printf_s("malloc error\n"); // free(lpReadContentByUTF8); // ::CloseHandle(hUtf8File); // return FALSE; //} //ZeroMemory(lpWriteContentByUTF16, cbWriteContentByUTF16); //if (0 == MultiByteToWideChar(CP_UTF8, 0, lpReadContentByUTF8, cbReadContentByUTF8, lpWriteContentByUTF16, cchWriteContentByUTF16)) //{ // printf_s("transform error\n"); // free(lpReadContentByUTF8); // free(lpWriteContentByUTF16); // ::CloseHandle(hUtf8File); // return FALSE; //} ////write UTF16LE encode file content //hUtf16leFile = ::CreateFile(lpUtf16leFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); //if (INVALID_HANDLE_VALUE == hUtf16leFile) //{ // printf("Terminal failure: Unable to write to file.\n"); // free(lpReadContentByUTF8); // free(lpWriteContentByUTF16); // ::CloseHandle(hUtf8File); // return FALSE; //} //for (int i = 0; i != cbWriteContentByUTF16 - 1; ++i) //{ // if (0 == lpWriteContentByUTF16[i] && 0 == lpWriteContentByUTF16[i + 1]) // { // cbWriteContentByUTF16 = i; // break; // } //} //////////////////////////////////////////////////////////////////////////// ////std::wstring wstrText = L"output to utf8 with BOM.\r\n输出到utf8带BOM的文件中。\r\nengli sh"; ////int lgText = wstrText.length(); ////int lgText2 = wstrText.size(); ////std::cout << lgText << "" << lgText2 << std::endl; //////////////////////////////////////////////////////////////////////////// //SetFilePointer(hUtf16leFile, 0, NULL, FILE_BEGIN); //if (FALSE == WriteFile(hUtf16leFile, lpWriteContentByUTF16, cbWriteContentByUTF16 * sizeof(WCHAR), &cbPreWriteContentByUTF16, NULL)) //{ // int errLogNumber = GetLastError(); // printf_s("error number:%d\n", errLogNumber); // free(lpReadContentByUTF8); // free(lpWriteContentByUTF16); // ::CloseHandle(hUtf16leFile); // ::CloseHandle(hUtf8File); // return FALSE; //} ////release resource //free(lpReadContentByUTF8); //free(lpWriteContentByUTF16); //::CloseHandle(hUtf16leFile); //::CloseHandle(hUtf8File); //return TRUE; } BOOL Utf8FileToUtf8NoBOMFile(CONST LPTSTR lpUtf8File, CONST LPTSTR lpUtf8NoBOMFile) { HANDLE hUtf8NoBOMFile = NULL; HANDLE hUtf8File = NULL; //create file hUtf8File = ::CreateFile(lpUtf8File, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hUtf8File) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); return FALSE; } //read UTF8 encode file content LPSTR lpReadContentByUTF8 = NULL; DWORD cbReadContentByUTF8 = 0; DWORD cbPreReadContentByUTF8 = 0; DWORD cchReadContentByUTF8 = 0; cbReadContentByUTF8 = SetFilePointer(hUtf8File, 0, NULL, FILE_END); if (INVALID_SET_FILE_POINTER == cbReadContentByUTF8) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); ::CloseHandle(hUtf8File); return FALSE; } lpReadContentByUTF8 = (CHAR *)malloc(cbReadContentByUTF8); if (NULL == lpReadContentByUTF8) { printf_s("malloc error\n"); ::CloseHandle(hUtf8File); return FALSE; } ZeroMemory(lpReadContentByUTF8, cbReadContentByUTF8); SetFilePointer(hUtf8File, 0, NULL, FILE_BEGIN); if (FALSE == ReadFile(hUtf8File, lpReadContentByUTF8, cbReadContentByUTF8, &cbPreReadContentByUTF8, NULL)) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); free(lpReadContentByUTF8); ::CloseHandle(hUtf8File); return FALSE; } if (sizeof(CHAR) != sizeof(BYTE)) { free(lpReadContentByUTF8); ::CloseHandle(hUtf8File); return FALSE; } cchReadContentByUTF8 = cbReadContentByUTF8; //write UTF8NoBOM encode file content LPSTR lpWriteContentByUTF8NoBOM = NULL; DWORD cbWriteContentByUTF8NoBOM = 0; DWORD cbPreWriteContentByUTF8NoBOM = 0; DWORD cchWriteContentByUTF8NoBOM = 0; cbWriteContentByUTF8NoBOM = cbReadContentByUTF8 - UTF8_SIGN; lpWriteContentByUTF8NoBOM = (CHAR *)malloc(cbWriteContentByUTF8NoBOM); if (NULL == lpWriteContentByUTF8NoBOM) { printf_s("malloc error\n"); free(lpReadContentByUTF8); ::CloseHandle(hUtf8File); return FALSE; } ZeroMemory(lpWriteContentByUTF8NoBOM, cbWriteContentByUTF8NoBOM); CopyMemory(lpWriteContentByUTF8NoBOM, lpReadContentByUTF8 + UTF8_SIGN, cbWriteContentByUTF8NoBOM); hUtf8NoBOMFile = ::CreateFile(lpUtf8NoBOMFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hUtf8NoBOMFile) { printf("Terminal failure: Unable to write to file.\n"); free(lpReadContentByUTF8); free(lpWriteContentByUTF8NoBOM); ::CloseHandle(hUtf8File); return FALSE; } SetFilePointer(hUtf8NoBOMFile, 0, NULL, FILE_BEGIN); if (FALSE == WriteFile(hUtf8NoBOMFile, lpWriteContentByUTF8NoBOM, cbWriteContentByUTF8NoBOM, &cbPreWriteContentByUTF8NoBOM, NULL)) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); free(lpReadContentByUTF8); free(lpWriteContentByUTF8NoBOM); ::CloseHandle(hUtf8NoBOMFile); ::CloseHandle(hUtf8File); return FALSE; } //release resource free(lpReadContentByUTF8); free(lpWriteContentByUTF8NoBOM); ::CloseHandle(hUtf8NoBOMFile); ::CloseHandle(hUtf8File); return TRUE; } BOOL Utf8NoBOMFileToUtf8File(CONST LPTSTR lpUtf8NoBOMFile, CONST LPTSTR lpUtf8File) { HANDLE hUtf8NoBOMFile = NULL; HANDLE hUtf8File = NULL; //create file hUtf8NoBOMFile = ::CreateFile(lpUtf8NoBOMFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hUtf8NoBOMFile) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); return FALSE; } //read UTF8 encode file content LPSTR lpReadContentByUTF8NoBOM = NULL; DWORD cbReadContentByUTF8NoBOM = 0; DWORD cbPreReadContentByUTF8NoBOM = 0; DWORD cchReadContentByUTF8NoBOM = 0; cbReadContentByUTF8NoBOM = SetFilePointer(hUtf8NoBOMFile, 0, NULL, FILE_END); if (INVALID_SET_FILE_POINTER == cbReadContentByUTF8NoBOM) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); ::CloseHandle(hUtf8NoBOMFile); return FALSE; } lpReadContentByUTF8NoBOM = (CHAR *)malloc(cbReadContentByUTF8NoBOM); if (NULL == lpReadContentByUTF8NoBOM) { printf_s("malloc error\n"); ::CloseHandle(hUtf8NoBOMFile); return FALSE; } ZeroMemory(lpReadContentByUTF8NoBOM, cbReadContentByUTF8NoBOM); SetFilePointer(hUtf8NoBOMFile, 0, NULL, FILE_BEGIN); if (FALSE == ReadFile(hUtf8NoBOMFile, lpReadContentByUTF8NoBOM, cbReadContentByUTF8NoBOM, &cbPreReadContentByUTF8NoBOM, NULL)) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); free(lpReadContentByUTF8NoBOM); ::CloseHandle(hUtf8NoBOMFile); return FALSE; } if (sizeof(CHAR) != sizeof(BYTE)) { free(lpReadContentByUTF8NoBOM); ::CloseHandle(hUtf8NoBOMFile); return FALSE; } cchReadContentByUTF8NoBOM = cbReadContentByUTF8NoBOM; //write UTF8NoBOM encode file content LPSTR lpWriteContentByUTF8 = NULL; DWORD cbWriteContentByUTF8 = 0; DWORD cbPreWriteContentByUTF8 = 0; DWORD cchWriteContentByUTF8 = 0; cbWriteContentByUTF8 = cbReadContentByUTF8NoBOM + UTF8_SIGN; lpWriteContentByUTF8 = (CHAR *)malloc(cbWriteContentByUTF8); if (NULL == lpWriteContentByUTF8) { printf_s("malloc error\n"); free(lpReadContentByUTF8NoBOM); ::CloseHandle(hUtf8NoBOMFile); return FALSE; } ZeroMemory(lpWriteContentByUTF8, cbWriteContentByUTF8); lpWriteContentByUTF8[0] = 0xef; lpWriteContentByUTF8[1] = 0xbb; lpWriteContentByUTF8[2] = 0xbf; CopyMemory(lpWriteContentByUTF8 + UTF8_SIGN, lpReadContentByUTF8NoBOM, cbWriteContentByUTF8 - UTF8_SIGN); hUtf8File = ::CreateFile(lpUtf8File, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hUtf8File) { printf("Terminal failure: Unable to write to file.\n"); free(lpWriteContentByUTF8); free(lpReadContentByUTF8NoBOM); ::CloseHandle(hUtf8NoBOMFile); return FALSE; } SetFilePointer(hUtf8File, 0, NULL, FILE_BEGIN); if (FALSE == WriteFile(hUtf8File, lpWriteContentByUTF8, cbWriteContentByUTF8, &cbPreWriteContentByUTF8, NULL)) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); free(lpWriteContentByUTF8); free(lpReadContentByUTF8NoBOM); ::CloseHandle(hUtf8NoBOMFile); ::CloseHandle(hUtf8File); return FALSE; } //release resource free(lpWriteContentByUTF8); free(lpReadContentByUTF8NoBOM); ::CloseHandle(hUtf8NoBOMFile); ::CloseHandle(hUtf8File); return TRUE; } BOOL Utf16leFileToUtf16beFile(CONST LPTSTR lpUtf16leFile, CONST LPTSTR lpUtf16beFile) { HANDLE hUtf16leFile = NULL; HANDLE hUtf16beFile = NULL; //create file hUtf16leFile = ::CreateFile(lpUtf16leFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hUtf16leFile) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); return FALSE; } //read UTF16le encode file content LPBYTE lpReadContentByUTF16le = NULL; DWORD cbReadContentByUTF16le = 0; DWORD cbPreReadContentByUTF16le = 0; DWORD cchReadContentByUTF16le = 0; cbReadContentByUTF16le = SetFilePointer(hUtf16leFile, 0, NULL, FILE_END); if (INVALID_SET_FILE_POINTER == cbReadContentByUTF16le) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); ::CloseHandle(hUtf16leFile); return FALSE; } if (0 != cbReadContentByUTF16le % 2) { printf_s("read byte error\n"); ::CloseHandle(hUtf16leFile); return FALSE; } lpReadContentByUTF16le = (BYTE *)malloc(cbReadContentByUTF16le); if (NULL == lpReadContentByUTF16le) { printf_s("malloc error\n"); ::CloseHandle(hUtf16leFile); return FALSE; } ZeroMemory(lpReadContentByUTF16le, cbReadContentByUTF16le); SetFilePointer(hUtf16leFile, 0, NULL, FILE_BEGIN); if (FALSE == ReadFile(hUtf16leFile, lpReadContentByUTF16le, cbReadContentByUTF16le, &cbPreReadContentByUTF16le, NULL)) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); free(lpReadContentByUTF16le); ::CloseHandle(hUtf16leFile); return FALSE; } //std::wstring wstrText(lpReadContentByUTF16le); //printf("%s\n", wstrText); if (sizeof(WCHAR) != sizeof(BYTE)*2) { free(lpReadContentByUTF16le); ::CloseHandle(hUtf16leFile); return FALSE; } //write UTF8NoBOM encode file content LPBYTE lpWriteContentByUTF16be = NULL; DWORD cbWriteContentByUTF16be = 0; DWORD cbPreWriteContentByUTF16be = 0; DWORD cchWriteContentByUTF16be = 0; cbWriteContentByUTF16be = cbReadContentByUTF16le; lpWriteContentByUTF16be = (BYTE *)malloc(cbWriteContentByUTF16be); if (NULL == lpWriteContentByUTF16be) { printf_s("malloc error\n"); free(lpReadContentByUTF16le); ::CloseHandle(hUtf16leFile); return FALSE; } ZeroMemory(lpWriteContentByUTF16be, cbWriteContentByUTF16be); CopyMemory(lpWriteContentByUTF16be, lpReadContentByUTF16le, cbWriteContentByUTF16be); for (DWORD i = 0; i < cbWriteContentByUTF16be; i += 2)//每两值交换 { lpWriteContentByUTF16be[i] = lpWriteContentByUTF16be[i] ^ lpWriteContentByUTF16be[i + 1]; lpWriteContentByUTF16be[i + 1] = lpWriteContentByUTF16be[i + 1] ^ lpWriteContentByUTF16be[i]; lpWriteContentByUTF16be[i] = lpWriteContentByUTF16be[i] ^ lpWriteContentByUTF16be[i + 1]; //BYTE hex_ = 0x0; //hex_ = lpWriteContentByUTF16be[i]; ////printf("%x\n", lpWriteContentByUTF16be[i]); //lpWriteContentByUTF16be[i] = lpWriteContentByUTF16be[i + 1]; //lpWriteContentByUTF16be[i + 1] = hex_; } hUtf16beFile = ::CreateFile(lpUtf16beFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hUtf16beFile) { printf("Terminal failure: Unable to write to file.\n"); free(lpWriteContentByUTF16be); free(lpReadContentByUTF16le); ::CloseHandle(hUtf16leFile); return FALSE; } SetFilePointer(hUtf16beFile, 0, NULL, FILE_BEGIN); if (FALSE == WriteFile(hUtf16beFile, lpWriteContentByUTF16be, cbWriteContentByUTF16be, &cbPreWriteContentByUTF16be, NULL)) { int errLogNumber = GetLastError(); printf_s("error number:%d\n", errLogNumber); free(lpWriteContentByUTF16be); free(lpReadContentByUTF16le); ::CloseHandle(hUtf16leFile); ::CloseHandle(hUtf16beFile); return FALSE; } //release resource free(lpWriteContentByUTF16be); free(lpReadContentByUTF16le); ::CloseHandle(hUtf16leFile); ::CloseHandle(hUtf16beFile); return TRUE; } BOOL Utf16beFileToUtf16leFile(CONST LPTSTR lpUtf16beFile, CONST LPTSTR lpUtf16leFile) { return Utf16leFileToUtf16beFile(lpUtf16beFile, lpUtf16leFile); } enum FileCodeType { OTHER = 0, UTF16LE, UTF8, UTF8_NO_BOM, UTF16BE }; //************************************ // Method: CodeFileAToCodeFileB // FullName: CodeFileAToCodeFileB // Access: public // Returns: bool // Qualifier:读取指定编码文件A的内容输出到指定编码文件B // Parameter: CONST LPTSTR lpFileA:输入文件 // Parameter: CONST FileCodeType wCodeTypeA:输入文件类型(包括:utf16le\utf16be\utf8\ANSI) // Parameter: CONST LPTSTR lpFileB:输出文件 // Parameter: CONST FileCodeType wCodeTypeB:输出文件类型(包括:utf16le\utf16be\utf8\utf8 without BOM\ANSI) //************************************ BOOL CodeFileAToCodeFileB(CONST LPTSTR lpFileA, CONST FileCodeType emCodeTypeA, CONST LPTSTR lpFileB, CONST FileCodeType emCodeTypeB) { BOOL bSUCCESS = FALSE; HANDLE hFileA = NULL; HANDLE hFileB = NULL; if (OTHER == emCodeTypeA || OTHER == emCodeTypeB) { return bSUCCESS; } if (NULL == lpFileA || NULL == lpFileB) { return bSUCCESS; } switch (emCodeTypeA) { case UTF16LE: { switch (emCodeTypeB) { case UTF16BE: { return Utf16leFileToUtf16beFile(lpFileA, lpFileB); } break; case UTF8: { return Utf16leFileToUtf8File(lpFileA, lpFileB); } break; case UTF8_NO_BOM: { return Utf16leFileToUtf8NoBOMFile2(lpFileA, lpFileB); } break; default:; } } break; case UTF8: { switch (emCodeTypeB) { case UTF16LE: { return Utf8FileToUtf16leFile(lpFileA, lpFileB); } break; case UTF16BE: { } break; case UTF8_NO_BOM: { return Utf8FileToUtf8NoBOMFile(lpFileA, lpFileB); } break; default:; } } break; case UTF8_NO_BOM: { switch (emCodeTypeB) { case UTF16LE: { return Utf8NoBOMFileToUtf16leFile(lpFileA, lpFileB); } break; case UTF8: { return Utf8NoBOMFileToUtf8File(lpFileA, lpFileB); } break; case UTF16BE: { } break; default:; } } break; case UTF16BE: { switch (emCodeTypeB) { case UTF16LE: { return Utf16beFileToUtf16leFile(lpFileA, lpFileB); } break; case UTF8: { } break; case UTF8_NO_BOM: { } break; default:; } } break; default:; } return bSUCCESS = TRUE; } void test__BYTETOCHAR(); void test__BYTETOWCHAR(); int _tmain(int argc, _TCHAR* argv[]) { CONST LPTSTR lpInputFileUTF8 = TEXT("input-utf8.txt"); CONST LPTSTR lpInputFileUTF16le = TEXT("input-utf16le.txt"); CONST LPTSTR lpInputFileUTF16be = TEXT("input-utf16be.txt"); CONST LPTSTR lpInputFileUTF8NoBOM = TEXT("input-utf8-no-bom.txt"); CONST LPTSTR lpOutputFileUTF8NoBOM = TEXT("output-utf8-no-bom.txt"); CONST LPTSTR lpOutputFileUTF8 = TEXT("output-utf8.txt"); CONST LPTSTR lpOutputFileUTF16le = TEXT("output-utf16le.txt"); CONST LPTSTR lpOutputFileUTF16be = TEXT("output-utf16be.txt"); //CodeFileAToCodeFileB(lpInputFileUTF16le, UTF16LE, lpOutputFileUTF8, UTF8); //CodeFileAToCodeFileB(lpInputFileUTF16le, UTF16LE, lpOutputFileUTF8NoBOM, UTF8_NO_BOM); //CodeFileAToCodeFileB(lpInputFileUTF8, UTF8, lpOutputFileUTF8NoBOM, UTF8_NO_BOM); //CodeFileAToCodeFileB(lpInputFileUTF8, UTF8, lpOutputFileUTF16le, UTF16LE); //CodeFileAToCodeFileB(lpInputFileUTF8NoBOM, UTF8_NO_BOM, lpOutputFileUTF16le, UTF16LE); //CodeFileAToCodeFileB(lpInputFileUTF8NoBOM, UTF8_NO_BOM, lpOutputFileUTF8, UTF8); //CodeFileAToCodeFileB(lpInputFileUTF16le, UTF16LE, lpOutputFileUTF16be, UTF16BE); //CodeFileAToCodeFileB(lpInputFileUTF16be, UTF16BE, lpOutputFileUTF16le, UTF16LE); //test__BYTETOCHAR(); test__BYTETOWCHAR(); return 0; } void test__BYTETOCHAR() { //TEXT("output to utf8 with BOM.\r\n输出到utf8带BOM的文件中。\r\nengli sh"); HANDLE hFileA = NULL; HANDLE hFileB = NULL; LPBYTE lpByte = NULL; INT cbByte = 0; INT cchByte = 0; LPSTR lpChar = NULL; INT cbChar = 0; INT cchChar = 0; hFileA = ::CreateFile(TEXT("input.txt"), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hFileA) return; cchByte = cbByte = SetFilePointer(hFileA, 0, NULL, FILE_END); if (INVALID_SET_FILE_POINTER == cbByte) return; lpByte = (BYTE *)malloc(cbByte); if (NULL == lpByte) return; ZeroMemory(lpByte, cbByte); SetFilePointer(hFileA, 0, NULL, FILE_BEGIN); if (FALSE == ReadFile(hFileA, lpByte, cbByte, NULL, NULL)) return; cchChar = cbChar = cbByte; lpChar = (CHAR *)malloc(cbChar); if (NULL == lpChar) return; ZeroMemory(lpChar, cbChar); for (INT i = 0; i != cbByte; ++i) { lpChar[i] = (CHAR)lpByte[i]; } ////////////////////////////////////////////////////////////////////////// std::string strText(lpChar); printf("%s\n", strText.c_str()); ////////////////////////////////////////////////////////////////////////// hFileB = ::CreateFile(TEXT("output.txt"), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); SetFilePointer(hFileB, 0, NULL, FILE_BEGIN); if (FALSE == WriteFile(hFileB, lpChar, cbChar, NULL, NULL)) return; free(lpByte); free(lpChar); CloseHandle(hFileA); CloseHandle(hFileB); } void test__BYTETOWCHAR() { HANDLE hFileA = NULL; HANDLE hFileB = NULL; LPBYTE lpByte = NULL; INT cbByte = 0; INT cchByte = 0; LPWSTR lpWChar = NULL; INT cbWChar = 0; INT cchWChar = 0; hFileA = ::CreateFile(TEXT("input.txt"), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hFileA) return; cchByte = cbByte = SetFilePointer(hFileA, 0, NULL, FILE_END); if (INVALID_SET_FILE_POINTER == cbByte) return; lpByte = (BYTE *)malloc(cbByte); if (NULL == lpByte) return; ZeroMemory(lpByte, cbByte); SetFilePointer(hFileA, 0, NULL, FILE_BEGIN); if (FALSE == ReadFile(hFileA, lpByte, cbByte, NULL, NULL)) return; cbWChar = cbByte; cchWChar = cbWChar / sizeof(WCHAR); lpWChar = (WCHAR *)malloc(cbWChar); if (NULL == lpWChar) return; ZeroMemory(lpWChar, cbWChar); CopyMemory(lpWChar, lpByte, cbWChar); ////////////////////////////////////////////////////////////////////////// std::wstring strText(lpWChar); wprintf(L"%s\n", strText.c_str()); ////////////////////////////////////////////////////////////////////////// hFileB = ::CreateFile(TEXT("output.txt"), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); SetFilePointer(hFileB, 0, NULL, FILE_BEGIN); if (FALSE == WriteFile(hFileB, lpWChar, cbWChar, NULL, NULL)) return; free(lpByte); free(lpWChar); CloseHandle(hFileA); CloseHandle(hFileB); } void test_CHARTOBYTE() { } void test__WCHARTOBYTE() { }
*注:由于较长建议大家放到编译器里面阅读。具体编码实例放到这里。
相关文章推荐
- 指定存储文件的编码格式(下)
- java文件读写操作指定编码格式
- java文件读写操作指定编码格式
- myeclipse下如何设置指定类型文件的编码格式
- c#读取并异步写入文件,简单版,指定编码,保持原格式。
- Java(Android)写文件时指定编码格式的问题
- Eclipse 对项目中特定文件使用指定编码格式
- php文件存储时的编码格式--utf8无BOM
- java文件读写操作指定编码格式[转]
- java文件读写操作指定编码格式
- Python write 写文件编码格式指定为utf-8
- java文件读写操作指定编码格式[转]
- Heritrix使用UTF-8编码格式存储文件
- 解决python ConfigParser文件编码问题(按指定格式存储文件(txt))
- 小时代5-Perl创建指定文件编码格式(如utf-8)
- 使用Perl创建指定编码格式(如utf-8)文件的实现代码
- java文件读写操作指定编码格式
- python指定文件编码格式
- java IO 指定输出文件的编码格式
- java文件读写操作指定编码格式