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

762 Views Asked by At

I am working on windows 10 system and using VS Code.

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 -o a" (program.c is the name of the C file). I store the output of a C file after typing the executable file name in the terminal as:

./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.

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.

This causes the git diff command to identify the two files as different:

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. Is there a way to automatically store the output in UTF-8 format?
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:

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.
If one changes the terminal to cmd and runs the following command the output file stores output in 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:

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. 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.

1

There are 1 best solutions below

0
On BEST ANSWER

It appears to have something to do with Windows PowerShell, and VSCode on Windows uses a PowerShell terminal by default.

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:

Change terminal