简体   繁体   English

默认 Output VS Code 中的文件存储在 UTF 16 LE 中,而默认设置为 UTF-8

[英]Default Output File in VS Code being stored in UTF 16 LE whereas default is set to UTF-8

I am working on windows 10 system and using VS Code.我正在使用 windows 10 系统并使用 VS 代码。

VS Code -v : 1.48.2
gcc version : gcc (MinGW.org GCC-8.2.0-5) 8.2.0

I am compiling a c program using " gcc program.c -oa " ( program.c is the name of the C file).我正在使用“ gcc program.c -oa ”编译一个 c 程序( program.c是 C 文件的名称)。 I store the output of a C file after typing the executable file name in the terminal as:在终端中键入可执行文件名后,我将 C 文件的 output 存储为:

./a > outputFile
1
2
4
6
7

The numbers from the second line through sixth line are the inputs, outputFile is the new file where I intend to store my output which would otherwise be displayed on the VS Code terminal itself.从第二行到第六行的数字是输入,outputFile 是我打算存储我的 output 的新文件,否则它会显示在 VS Code 终端本身上。

My problem is the output is being stored in UTF-16 LE format while my default encoding in VS code is set to UTF-8.我的问题是 output 以 UTF-16 LE 格式存储,而我在 VS 代码中的默认编码设置为 UTF-8。

This causes the git diff command to identify the two files as different:这会导致git diff命令将这两个文件识别为不同的:

git diff outputFile expectedOutput

gives the message:给出信息:

binary files a/outputFile b/expectedOutput differ

whereas:然而:

fc.exe outputFile expectedOutput

gives the message:给出信息:

Resync Failed.Files are too different.

I am required to manually change the encoding of my outputFile to UTF-8 after which the commands identify the files being identical.我需要手动将我的 outputFile 的编码更改为 UTF-8,之后命令将文件识别为相同。 Is there a way to automatically store the output in UTF-8 format?有没有办法将output自动存储为UTF-8格式?
Reproducible Example:(Major Edit)可重现的例子:(主要编辑)

#include <stdio.h>
void main()
{
    printf("1 2 3 4\n");
}//this is a test program named test.c

If the VSCode Terminal which as pointed out by Bernard is Powershell by Default and the following sequence of commands is run:如果 Bernard 指出的 VSCode 终端默认为Powershell并且运行以下命令序列:

gcc test.c
./a > out1.txt

The output file contains output in UTF-16LE format which can be seen by opening the text file in Notepad and trying SAVE-AS. output 文件包含 UTF-16LE 格式的 output,可以通过在记事本中打开文本文件并尝试另存为来查看。
If one changes the terminal to cmd and runs the following command the output file stores output in UTF-8:如果将终端更改为cmd并运行以下命令,则 output 文件会将 output 存储在 UTF-8 中:

gcc test.c
a.exe > out2.txt

Same happens if the git bash terminal is chosen and following commands are executed the output is stored in UTF-8:如果选择git bash终端并执行以下命令,也会发生同样的情况 output 存储在 UTF-8 中:

gcc test.c
./a > out3.txt

out1.txt contains output in UTF-16LE format and out2.txt,out3.txt contains output in UTF-8 format. out1.txt 包含 UTF-16LE 格式的 output 和 out2.txt,out3.txt 包含 UTF-8 格式的 output。 Now I can change the default terminal and get rid of this issue.现在我可以更改默认终端并摆脱这个问题。 Methods to fix this in Powershell may be useful, though a solution is found as suggested by Bernard by changing default terminal . Powershell 中修复此问题的方法可能很有用,但根据伯纳德的建议,通过更改默认终端找到了解决方案。

It appears to have something to do with Windows PowerShell, and VSCode on Windows uses a PowerShell terminal by default.它似乎与Windows PowerShell有关,Windows上的VSCode默认使用PowerShell终端。

It isn't clear what the underlying reason for this issue is, but there is a simple workaround – just change the VSCode terminal to the standard Windows terminal instead of PowerShell:目前尚不清楚此问题的根本原因是什么,但有一个简单的解决方法——只需将 VSCode 终端更改为标准的 Windows 终端而不是 PowerShell:

换终端

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM