在每次调试程序时,每次运行程序都要输入一遍测试数据,太麻烦了。
使用freopen重定向输入操作,将输入由键盘重定向到文本文件,运行程序时不再需要重复输入测试数据。完成调试程序后,只需要注释掉freopen这行代码就可以了。
#include "stdafx.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int x = 0, y = 0;
//test
freopen(".\\test.txt", "r", stdin);
//cin >> x >> y; //如果全部读入返回true,否则false
scanf("%d%d", &x, &y); //返回读取的个数,否则返回EOF(-1)
printf("x = %i, y = %i\n", x, y);
return 0;
}