win32 - MultiByteToWideChar的示例
作者:互联网
该函数经常被用来处理UTF-8和ANSI格式的字符串,将它们转换为宽字节(UTF-16)
#include <iostream> #include <Windows.h> #include <string> #define MAXBUFFERSIZE 1024 using namespace std; int main() { DWORD windowStart = 0; DWORD windowEnd = 15; DWORD windowSize = 15; // buffer window containing tag candidate wstring windowCopy; DWORD bufferSize = MAXBUFFERSIZE; _int64 fileRemaining; HANDLE hFile; DWORD dwBytesRead = 0; OVERLAPPED ol = { 0 }; LARGE_INTEGER dwPosition; TCHAR* buffer = new TCHAR[MAXBUFFERSIZE];; hFile = CreateFileW( L"test.txt", // file to open 其中包含UTF-8格式或者ANSI格式的文本 GENERIC_READ, // open for reading FILE_SHARE_READ | FILE_SHARE_WRITE, // share for reading and writing NULL, // default security OPEN_EXISTING, // existing file only FILE_ATTRIBUTE_NORMAL, // normal file | FILE_FLAG_OVERLAPPED NULL); // no attr. template if (hFile == INVALID_HANDLE_VALUE) { wprintf(L"CreateFile"); return 0; } LARGE_INTEGER size; GetFileSizeEx(hFile, &size); _int64 fileSize = (__int64)size.QuadPart; double gigabytes = fileSize * 9.3132e-10; wprintf(L"file size: %lld bytes \(%.1f gigabytes\)\n", fileSize, gigabytes); fileRemaining = fileSize; wprintf(L"file remaining: %lld bytes\n", fileRemaining); while (fileRemaining) { if (bufferSize > fileRemaining) bufferSize = fileRemaining; memset(buffer, 0, fileSize); if (FALSE == ReadFile(hFile, buffer, bufferSize, &dwBytesRead, NULL)) { wprintf(L"file read failed\n"); CloseHandle(hFile); return 0; } fileRemaining -= bufferSize; wprintf(L"outer loop\n"); while (windowEnd < bufferSize) { windowSize = windowEnd - windowStart; wstring str(buffer); wstring sub = str.substr(windowStart, windowSize); TCHAR converted[60] = { 0 }; MultiByteToWideChar(CP_ACP, MB_COMPOSITE, (LPCCH)sub.c_str(), -1, converted, 60); wprintf(L"windowStart:%d windowEnd:%d char:%s\n", windowStart, windowEnd, converted); windowStart = windowEnd; windowEnd+=windowSize; } } delete buffer; }
标签:windowEnd,windowStart,示例,wprintf,win32,MultiByteToWideChar,file,hFile,fileRemain 来源: https://www.cnblogs.com/strive-sun/p/14047760.html