C++写的程序里有cin输入的地方,运行时总是打一遍太麻烦,想把输入写在一个文本中,利用shell代替手打应该怎么写?
用expect脚本 但是这个需要精确输入提示符
比如自动telnet脚本:
#!/usr/bin/expect
spawn telnet 127.0.0.1
expect "username:"
send "username/r"
expect "Password:"
send "password/r"
int main()
{
freopen("sample.in", "r", stdin);
freopen("sample.out", "w", stdout);
/* 同控制台输入输出 */
int mainIndex = 0;
scanf("%d",&mainIndex);
for (int i = 0; i < mainIndex;i++)
{
int step = 0;
int N = 0;
scanf("%d %d",&N,&step);
// 下面申请内存时候要用sizeof不然free时候会算错导致堆出错
int *array = (int*)malloc(sizeof(int)*N*N);
for (int j = 0;j<N*N;j++)
{
scanf("%d",array+j);
}
printf("%d\n",getMax(N,step,array));
free(array);
}
fclose(stdin);
fclose(stdout);
return 0;
}
或者你可以利用标准输入重定向加快测试过程:
freopen("c:\input.txt", "r", stdin);