红联Linux门户
Linux帮助

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

发布时间:2016-05-29 10:06:07来源:linux网站作者:happy_xiahuixiax

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.这里要注意的就是使用重定向的时候,输入文件外面的两个尖括号不能少。


本文永久更新地址:http://www.linuxdiyf.com/linux/21046.html