如何打印一个字符串作为stdin流的输入?

时间:2019-04-25 20:20:45

标签: python c linux gdb sh

如何使用例如带printf的python传递标准输入流自定义字符串?在启动可执行文件之前,我无法执行此操作,因为在执行其他一些输入后,我需要执行此操作。

代码是:

void play(){
    int guess=0;
    char buf[CHOICEBUFSIZE];
    long long int attempt=0;
    time_t timer;
    int value;

    time(&timer);
    timer /= 60;
    timer *= 60;

    //printf("<<<<<< Time: %t\n", timer);

    srand(timer);

    do {
        attempt += 1;
        value = rand();
        puts(" > Gimme the magic number or 0 to gtfo!");
        printf(" < ");

        //RIGHT DOWN HERE 
        fgets(buf, CHOICEBUFSIZE, stdin);
        guess = atoi(buf);

    printf("%d\n", value);
        if (guess == value){
            win(attempt);
            return;
        }
        else {
            puts(" > Not even close!\n > Try again!");
        }
    } while (guess);

}

这是一个显示需要输入的屏幕。

先谢谢了。我敢肯定答案很简单,但找不到与我的案子类似的东西。

1 个答案:

答案 0 :(得分:0)

stdin是输入流,因此您无法对其进行写入。既然听起来您正在尝试并行执行两项任务,并且让一项任务与另一项任务进行通信,那么您可能需要考虑使用pthreads或fork()

相关问题