在 Visual Studio 2019 年进入标准 wstdio 函数
step into standard wstdio functions in Visual Studio 2019
我想找出使用新功能 setlocale(LC_ALL, ".utf8")
时标准函数 fgetwc()
无法从 utf8 文本文件读取 '\u2013'
(EN DASH) 而是 returns WEOF
。也许找到解决方法。
我禁用了“仅我的代码”并为包含 fgetwc
的 C:\WINDOWS\SysWOW64\ucrtbased.dll
启用了符号下载
但是,当我尝试进入该功能时,它找不到 fgetwc.cpp
.
这两个位置不包含该文件,我找不到任何其他位置:
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\crt\src\
C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.28.29333\crt\src\
这是我的测试程序:
#include <stdio.h>
#include <locale.h>
#include <wchar.h>
#include <stdlib.h>
int main()
{
wint_t wc; // = L'\u2013';
FILE* file;
printf("%s\n", setlocale(LC_ALL, ".utf8"));
file = fopen("test.txt", "r");
wc = fgetwc(file);
// ffff '?' 0 0
fprintf(stdout, "%04x '%lc' %d %d\n", wc, wc, ferror(file), feof(file));
return 0;
}
它打印 ffff
而不是 2013
。 ferror()
和 feof()
return 错误。
test.txt:
–
编码为E2 80 93
为了读取 UTF-8 文件,可选择删除 setlocale
调用,并将 fopen
行替换为:
file = fopen("test.txt", "r, ccs=utf-8");
ccs=encoding -- Specifies the encoded character set to use (one of UTF-8, UTF-16LE, or UNICODE) for this file. Leave unspecified if you want ANSI encoding.
这似乎暗示必须明确指定 ccs=UTF-8
编码才能将文件读取为 UTF-8 文本。
不过,另一方面,“ANSI”曾经表示活动代码页或系统默认语言环境。随着最近 Windows 10 1903 及更高版本对 UTF-8 作为活动代码页的支持,预计“ANSI 编码”与“[=36”相同=]UTF-8 编码”,当当前语言环境是 UTF-8 时。然而,当前实施的 UCRT 似乎并非如此。
用于写入宽字符,#include <io.h>
和 <fcntl.h>
,并将 fprintf
行替换为:
_setmode(_fileno(stdout), _O_U16TEXT);
wprintf(L"%04x '%wc' %d %d\n", wc, wc, ferror(file), feof(file));
wprintf is a wide-character version of printf; format is a wide-character string. wprintf and printf behave identically if the stream is opened in ANSI mode. printf does not currently support output into a UNICODE stream.
我想找出使用新功能 setlocale(LC_ALL, ".utf8")
时标准函数 fgetwc()
无法从 utf8 文本文件读取 '\u2013'
(EN DASH) 而是 returns WEOF
。也许找到解决方法。
我禁用了“仅我的代码”并为包含 fgetwc
的 C:\WINDOWS\SysWOW64\ucrtbased.dll
启用了符号下载
但是,当我尝试进入该功能时,它找不到 fgetwc.cpp
.
这两个位置不包含该文件,我找不到任何其他位置:
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\crt\src\
C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.28.29333\crt\src\
这是我的测试程序:
#include <stdio.h>
#include <locale.h>
#include <wchar.h>
#include <stdlib.h>
int main()
{
wint_t wc; // = L'\u2013';
FILE* file;
printf("%s\n", setlocale(LC_ALL, ".utf8"));
file = fopen("test.txt", "r");
wc = fgetwc(file);
// ffff '?' 0 0
fprintf(stdout, "%04x '%lc' %d %d\n", wc, wc, ferror(file), feof(file));
return 0;
}
它打印 ffff
而不是 2013
。 ferror()
和 feof()
return 错误。
test.txt:
–
编码为E2 80 93
为了读取 UTF-8 文件,可选择删除
setlocale
调用,并将fopen
行替换为:file = fopen("test.txt", "r, ccs=utf-8");
ccs=encoding -- Specifies the encoded character set to use (one of UTF-8, UTF-16LE, or UNICODE) for this file. Leave unspecified if you want ANSI encoding.
这似乎暗示必须明确指定
ccs=UTF-8
编码才能将文件读取为 UTF-8 文本。不过,另一方面,“ANSI”曾经表示活动代码页或系统默认语言环境。随着最近 Windows 10 1903 及更高版本对 UTF-8 作为活动代码页的支持,预计“ANSI 编码”与“[=36”相同=]UTF-8 编码”,当当前语言环境是 UTF-8 时。然而,当前实施的 UCRT 似乎并非如此。
用于写入宽字符,
#include <io.h>
和<fcntl.h>
,并将fprintf
行替换为:_setmode(_fileno(stdout), _O_U16TEXT); wprintf(L"%04x '%wc' %d %d\n", wc, wc, ferror(file), feof(file));
wprintf is a wide-character version of printf; format is a wide-character string. wprintf and printf behave identically if the stream is opened in ANSI mode. printf does not currently support output into a UNICODE stream.