VS Code: standard input and output file redirection

VS Code: standard input and output file redirection


1. Background

  During C / C ++ programming, sometimes need to enter data into .exe, but has too much data or complex, in particular the need to re-enter many times when debugging, very troublesome. Now hope .exe can automatically input data and save the results.

2. Test file

  Now VS Code directory has three documents, namely: input, main.cpp, output. main.cpp needs to read from the input data, calculates the sum of two input values and save the result to the output.
input content:

1 2

main.cpp Code:

#include <iostream>
using namespace std;

//计算两个输入值的和
int main()
{
    int x, y;
    cin >> x >> y;
    cout << x << " + " << y << " = " << x + y << endl;
    return 0;
}

3. file redirection

  Most operating systems support file redirection, it will be the standard input and standard output associated with the file name.
File redirection command is generally as follows:

***.exe <infile >outfile

cmd command parsing:

// 跳转到main.cpp所在文件夹
cd C:\Users\wyh\Desktop\2  
// 编译main.cpp,生成main.exe
g++ main.cpp -o main.exe
// 文件重定向
main.exe <infile >outfile
// 输出outfile
type outfile

cmd command redirection results:
cmd file redirection results

  VS Code terminal actually using Windows PowerShell. PowerShell Support ">" does not support "<." It needs to obtain the contents of the input file through the "Get-Content".

PowerShell Windows:
Terminal VS Code
Windows PowerShell using the "<" error:
Here Insert Picture Description

PowerShell redirect command:

Get-Content infile | ./***.exe >outfile

PowerShell redirection result:
PowerShell redirection result

4. Summary

  1. File redirection can automatically read the data, save the results.

5. References

  1. How to redirect standard input \ output WindowsPowerShell in?
Published 77 original articles · won praise 25 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_34801642/article/details/103795042