c - 从C中的文件中读取unicode字符
问题描述
我正在尝试从 .csv 文件中读取 UTF-8 字符串,然后将其写入控制台。
a.csv内容:
Gijón
在对该主题进行了一整天的研究后,我发现执行此类操作的所谓正确方法应该类似于以下内容:
int main(int argc, char *argv[])
{
char *locale = setlocale(LC_ALL, "");
printf("locale: %s\n", locale);
const int MAX_LINE_SIZE = 1024;
char line[MAX_LINE_SIZE];
wchar_t wline[MAX_LINE_SIZE];
// Attempt 0: no special handling
FILE* stream = fopen("a.csv", "r");
fgets(line, MAX_LINE_SIZE, stream);
printf("%s\n", line); // Expected to print "Gijón", prints "Gijón"
fclose(stream);
// Attempt 1: mbstowcs
mbstowcs(wline, line, MAX_LINE_SIZE);
wprintf(L"%ls\n", wline); // Expected to print "Gijón", prints "Gijón"
// Attempt 2: fgetws
stream = fopen("a.csv", "r");
fgetws(wline, MAX_LINE_SIZE, stream);
wprintf(L"%ls\n", wline); // Expected to print "Gijón", prints "Gijón"
fclose(stream);
// Attempt 3: _wfopen
stream = _wfopen(L"a.csv", L"rb");
fgetws(wline, MAX_LINE_SIZE, stream);
wprintf(L"%ls\n", wline); // Expected to print "Gijón", prints ""
fclose(stream);
// Printing command line parameter
mbstowcs(wline, argv[1], MAX_LINE_SIZE);
wprintf(L"%ls\n", wline); // Properly prints "Gijón"
}
但是运行这个程序会导致:
.\myprogram.exe Gijón
locale: Spanish_Spain.1252
Gijón
Gijón
Gijón
我不认为这是控制台本身的问题,因为argv[1]
转换工作正常。
我错过了什么?
解决方案
wchar_t
和宽字符函数(wfopen
等)主要在 Windows 中用于处理 UTF16 编码中的 Unicode。
UTF8 使用char
和相同的 ASCII 兼容的 C 函数(fopen
等) 要读取 UTF8,您可以对 ASCII 使用相同的 C 函数。
Windows 不完全支持读取和显示 UTF8,因此您必须在 UTF8 和 UTF16 之间进行转换才能正确显示文本。Windows 10 确实支持控制台 Windows 的 UTF8,请参阅相关主题。
#include <stdio.h>
#include <windows.h>
int main(void)
{
const char* filename = "a.csv";
FILE* fp = fopen(filename, "r");
char buf[1000];
fgets(buf, sizeof(buf), fp);
if(strlen(buf) > 2)
if(strncmp(buf, "\xFF\xFE", 2) == 0)
{
printf("UTF16-LE\n");
fclose(fp);
fp = fopen(filename, "rb");
wchar_t wbuf[1000] = { 0 };
fgets((char*)wbuf, sizeof(buf), fp);
MessageBoxW(0, wbuf, L"UTF16-LE", 0);
return 0;
}
if(strlen(buf) > 3)
if(strncmp(buf, "\xEF\xBB\xBF", 3) == 0)
printf("UTF8 with BOM\n");
//assume UTF8 and convert to UTF16:
int size = MultiByteToWideChar(CP_UTF8, 0, buf, -1, NULL, 0);
wchar_t *utf16 = malloc((size + 1) * sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8, 0, buf, -1, utf16, size);
MessageBoxA(0, buf, "ANSI", 0);
MessageBoxW(0, utf16, L"UTF8 converted", 0);
return 0;
}
如果源文件是 UTF8,那么您基本上将其视为 ASCII。请注意strtok
不能处理 ASCII 范围之外的输入字符之类的函数。唯一的其他复杂情况是当您尝试在 Windows 中打印它时。将以下示例与自定义printf
函数一起使用:
void printf_utf8(const char* format, ...)
{
va_list args;
va_start(args, format);
int len = _vscprintf(format, args) + 1;
char *buf = malloc(len);
vsprintf(buf, format, args);
//convert to UTF16 and print
int wbuf_size = MultiByteToWideChar(CP_UTF8, 0, buf, -1, NULL, 0);
wchar_t *wbuf = malloc((wbuf_size + 1) * sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8, 0, buf, -1, wbuf, wbuf_size);
DWORD temp;
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
WriteConsoleW(h, wbuf, wcslen(wbuf), &temp, 0);
free(wbuf);
free(buf);
}
int main(void)
{
FILE* fp = fopen("a.csv", "r");
if(!fp)
return 0;
char buf[1000];
fgets(buf, sizeof(buf), fp);
printf_utf8("Test %s %d\n", buf, 123);
return 0;
}
推荐阅读
- sql-server - 如何在 SQL Server 2012 中计算运行总计
- javascript - 带有两个条件的循环始终为真
- angular - 使用 [routerLink] 代替带有简单 url 的 routerLink 有什么好处
- git - 尝试通过 SSH 克隆时“似乎不是 git 存储库”
- r - 为什么 dmultinom MLE 的优化在 R 中错误地收敛?
- azure - 我们应该如何在远程 IoT Edge 部署中管理 Moby/Docker 映像?
- python - 如何从 .txt 文件加载 json 对象列表?
- c# - 从 Azure 容器和子目录中获取所有 blob Uris
- r - 使用特定行中的值过滤 R 中的列
- java - holder.btn_email.setOnClickListener