给使用scanf获得输入的程序写测试

有一大坨命令行程序,其中大量使用scanf获得输入,不断输入,然后打印结果,现在想使用类似与echo "1 2 3 n hello > myprogram
实际操作为./myprogram然后等待开始输入,然后按1,按ENTER,按2,再按ENTERT,再按3,再按ENTERT,再按n,再按ENTERT,最后按hello,最后ENTER解释程序。

阅读 3.3k
2 个回答

当输入有很多Enter时,用 myprogram < input.txt方式更方便

下面是一个小例子

input.c

#include<stdio.h>

int main()
{
    int num, a ;
    char c[2000];
    printf("input start\n");
    scanf("%d", &num);
    scanf("%d", &a);
    scanf("%s", c);
    printf("this is what you input\n");
    printf("%d\n", num);
    printf("%d\n", a);
    printf("%s\n", c);
    return 0;
}

input.txt

123
456
abcdefghnopq
$ gcc input.c
$ ./a.out <input.txt
input start
this is what you input
123
456
abcdefghnopq

使用 echo 也可以
你也可以写到文件里面,然后重定向标准输入

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题