我正在使用 Windows 10 系统并使用 VS Code。
VS Code -v : 1.48.2
gcc version : gcc (MinGW.org GCC-8.2.0-5) 8.2.0
我正在使用“ gcc program.c -o a
”(program.c
是 C 文件的名称)编译 ac 程序。在终端中键入可执行文件名后,我将 C 文件的输出存储为:
./a > outputFile
1
2
4
6
7
第二行到第六行的数字是输入,outputFile 是我打算存储输出的新文件,否则它将显示在 VS Code 终端本身上。
我的问题是输出以 UTF-16 LE 格式存储,而我在 VS 代码中的默认编码设置为 UTF-8。
这会导致git diff
命令将两个文件识别为不同的:
git diff outputFile expectedOutput
给出信息:
binary files a/outputFile b/expectedOutput differ
然而:
fc.exe outputFile expectedOutput
给出信息:
Resync Failed.Files are too different.
我需要手动将 outputFile 的编码更改为 UTF-8,之后命令会识别出相同的文件。有没有办法自动以 UTF-8 格式存储输出?
可重现的示例:(主要编辑)
#include <stdio.h>
void main()
{
printf("1 2 3 4\n");
}//this is a test program named test.c
如果 Bernard 指出的 VSCode 终端默认为Powershell,并且运行以下命令序列:
gcc test.c
./a > out1.txt
输出文件包含 UTF-16LE 格式的输出,可以通过在记事本中打开文本文件并尝试 SAVE-AS 来查看。
如果将终端更改为cmd并运行以下命令,则输出文件以 UTF-8 格式存储输出:
gcc test.c
a.exe > out2.txt
如果选择git bash终端并执行以下命令,输出存储在 UTF-8 中,也会发生同样的情况:
gcc test.c
./a > out3.txt
out1.txt 包含 UTF-16LE 格式的输出,out2.txt,out3.txt 包含 UTF-8 格式的输出。现在我可以更改默认终端并摆脱这个问题。在 Powershell 中解决此问题的方法可能很有用,尽管按照 Bernard 的建议通过更改默认终端找到了解决方案。