'Enter is converted to gibberish code after system()

When I use C++ to invoke Python program output (By system command with parameters), it outputs gibberish code at the end of line. After that, I couldn't input any character (Include Backspace and Enter), it displays a hollow square.

Console screenshot: https://user-images.githubusercontent.com/53416099/162125240-8d736a41-2e11-4d9c-a873-1257d3585cd7.png (I cannot post images)

Whole function code: (Uses file process)

string info;
cin >> info;
info = "TRANS -i \"" + info + "\" > WCH_TRANS.tmp";
system(info.c_str());
cmd_line = false;
Sleep(2000);
fin >> info;
cout << info << endl;
DeleteFile("WCH_TRANS.tmp");

I have written an alike function like this, but it didn't display gibberish code.



Solution 1:[1]

After repeated inspection, I have found that after invoking system() can cause these encoding problems.

You can redirect the output of the function to a temporarily file like this.

void WCH_RunSystem(string str) {
    // Run system command.
    freopen("WCH_SYSTEM.tmp", "w", stdout);
    system(str.c_str());
    freopen("CON", "w", stdout);
    Sleep(500);
    DeleteFile("WCH_SYSTEM.tmp");
}

Solution 2:[2]

Can you try UTF-8? Maybe the python's character is diffrent from cpp.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yuchen Ren
Solution 2 jinshuhang