gpt4 book ai didi

WriteFile(...) changes file encoding from UTF-16 LE to UTF-8(写入文件(...)将文件编码从UTF-16 LE更改为UTF-8)

转载 作者:bug小助手 更新时间:2023-10-25 14:36:20 32 4
gpt4 key购买 nike



I have UTF-16 LE file. If I write the text to .txt file, it becomes UTF-8.
Code:

我有UTF-16 LE档案。如果我将文本写入.txt文件,它将变成UTF-8。代码:


hFile = CreateFile(TEXT("D:\\clown.txt"), GENERIC_READ | GENERIC_WRITE,  FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
wsprintf(buffer, TEXT("ERROR CODE: %i"), GetLastError());
MessageBox(NULL, buffer, TEXT("Error"), MB_OK | MB_ICONERROR);
return 0;
}
MessageBox(NULL, TEXT("hFile was created"), TEXT("Successfully"), MB_OK);
sBuff = GetWindowText(hEdit, buffer, BUFFER_SIZE);
if (!WriteFile(hFile, buffer, sBuff * 2, &i, NULL))
{
wsprintf(buffer, TEXT("ERROR CODE: %i"), GetLastError());
MessageBox(NULL, buffer, TEXT("Error"), MB_OK | MB_ICONERROR);
return 0;
}
wsprintf(buffer, TEXT("%i"), GetLastError());
MessageBox(NULL, buffer, TEXT("Successfully"), MB_OK);
CloseHandle(hFile);
return 0;

WriteFileEx have the same problem

WriteFileEx也有同样的问题


更多回答

WriteFile has no idea of what a character or an encoding is, it only writes bytes. You're not writing UTF8 nor UTF16, you're writing whatever you got into buffer, directly into the file, as a bunch of bytes. Check this for some info about what a BOM is learn.microsoft.com/en-us/windows/win32/intl/… and this stackoverflow.com/questions/28618715/…

WriteFile不知道字符或编码是什么,它只写字节。你不是在写UTF8或UTF16,而是把你得到的东西直接写进缓冲区,以一串字节的形式写进文件。有关物料清单是learn.microsoft.com/en-us/windows/win32/intl/…的一些信息,请查看此链接这个Stackoverflow.com/Questions/28618715/…

How do you know that the file is UTF-8? There is no code shown that reads the file. Also, the use of the TEXT macro raises the possibility that you are accidentally compiling in ANSI rather than Unicode.

你怎么知道这个文件是UTF-8?没有显示读取该文件的代码。此外,使用TEXT宏会增加意外使用ANSI而不是Unicode进行编译的可能性。

优秀答案推荐

WriteFile (and WriteFileEx) accept raw binary data and pass it to the device identified by the hFile parameter. No attempt is made to transcode or even interpret the data. The device receives a byte-for-byte copy of the input buffer.

WriteFileEx(和WriteFileEx)接受原始二进制数据,并将其传递给由hFile参数标识的设备。不会尝试对数据进行代码转换,甚至不会进行解释。该设备接收输入缓冲器的逐字节副本。


When calling WriteFile on a file handle and the file contents aren't what you expected, the issue is with the input to WriteFile rather than an issue with the API itself. The input comes from a call to GetWindowText, and the problem is here, in all likelihood.

当在文件句柄上调用WriteFile时,文件内容并不是您所期望的,问题出在WriteFile的输入上,而不是API本身。输入来自对GetWindowText的调用,问题很可能就在这里。


GetWindowText is a preprocessor macro that expands to either GetWindowTextW or GetWindowTextA depending on whether the UNICODE preprocessor symbol is defined. Since the code isn't self-sufficient, I'm going to have to guess here:

GetWindowText是一个预处置宏,根据是否定义了Unicode预处理器符号,它可以扩展为GetWindowTextW或GetWindowTextA。由于代码不是自给自足的,我将不得不在这里猜测:



  • The code is compiled using a C compiler

  • The code author ignores compiler warnings

  • UNICODE is not defined


Since we don't know with certainty what the problem is, the following is an educated guess: To solve the issue, replace GetWindowText with GetWindowTextW.

由于我们不能确切地知道问题是什么,以下是一个有根据的猜测:要解决该问题,请将GetWindowText替换为GetWindowTextW。


更多回答

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com