Vs2017 uses cout garbled code, and uses printf to output normally.

problem description

vs2017 uses cout garbled code, and uses printf to output normally

the platform version of the problem and what methods you have tried

modify vs source files to gbk and utf, but still garbled

related codes

-sharpinclude<cstdio>
-sharpinclude<iostream>
int main() {
    std::cout << "" << std::endl;
    printf("\n");
    system("pause");
    return 0;
}

result

clipboard.png

CPP
Apr.06,2021

has never encountered this situation. You debug it and have a look. Try changing the


character set setting to multiple characters.
General- > Character Set- > Use Multi-Byte Character Set. (VS2010)


still not used after trying


in CPP, characters other than ASCII characters are wide and need to be identified by L . The wide character stream has a specific output stream, which is std::wcout . Therefore, the correct output mode is std::wcout < < L "Hello" < < std::endl .

in addition, you need to refer to the concept of std::locale . std::locale represents a collection of settings for a region. locale document

each input and output stream can be bound to a std::locale . The default binding is std::locale ("C") . std::wcout.imbue ( locale ) is used here, and the region to be set is zh-cn . I did not find the relevant content. This region string may be related to the environment.

the final code is:

-sharpinclude <cstdio>
-sharpinclude <locale>
-sharpinclude <string>
-sharpinclude <iostream>

int main() {
    auto old_locale = std::wcout.imbue(std::locale("zh-cn"));
    std::wcout << L"" << std::endl;

    std::cout << "old locale: " << old_locale.name() << std::endl;
    auto new_locale = std::wcout.imbue(old_locale);
    std::cout << "new locale: " << new_locale.name() << std::endl;

    printf("\n");

    system("pause");
}

output is:


old locale: C
new locale: zh-cn

this part is complicated and I don't know much about it. I hope it will be helpful to you.

Menu