您的位置:首页 > 运维架构 > Linux

linux下程序调试使用文件重定向

2016-04-01 12:17 645 查看
c++ primer 5th教给我的小技巧:

usinng the file redirection.

It can be tedious to repeatedly type these transactions as input to the program you are testing . Most OS support file redirection , which let us associate a named file with the standard input and the standard output , for example :

(the test is a program that can run under linux)

test.cpp

#include <iostream>
using namespace std;

int main()
{
int a;
while ( cin >>  a)
{
cout << a;
}
return 0;
}


g++ -o test test.cpp

新建一个输入文件:nano input /nano为linux新建文件的一个命令,nano也是一个行编辑器/

在input文件中输入:

1 2 3 4 5

新建一个输出文件:nano output

运行程序:./test output

打开output看一下里面的内容:nano output

12345

OK.这里要注意的就是使用重定向的时候,输入文件外面的两个尖括号不能少。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息