在通过 scanf() 等待输入之前没有通过 SSH 输出 [英] No output over SSH before waiting for input via scanf()

查看:43
本文介绍了在通过 scanf() 等待输入之前没有通过 SSH 输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在远程 Unix 系统上有一个名为 get_int.c 的文件,其中包含以下内容:

#include int main() {整数输入;printf("给一个整数:");fflush(标准输出);scanf("%d", &input);printf("再试一次:");scanf("%d", &input);printf("你说... %d\n", input);返回0;}

我有一个命令可以从我的本地 WSL 编译和运行这个文件:

sshpass -f pass.txt ssh username@remote.host.address "cd path/to/file/&& gcc get_int.c && a.out">

当我执行这个命令时,我成功得到提示Give an integer: 并提供一个并按回车键.但是,我没有收到提示 Try again:.我仍然可以输入一个整数 (123) 并按 Enter.当我这样做时,它会打印 Try again: You said... 123

如您所见,在我 fflush(stdout) 或程序结束之前,不会进行打印.我可以修改我的 ssh 命令,以便输出到本地终端,而不必在每次 scanf 之前 fflush 吗?

解决方案

输出到 stdout 在特定的系统上从 stdin 读取时似乎没有被刷新情况描述.在每次调用 scanf() 之前,您必须使用 fflush() 显式刷新 stdout:

#include int main() {整数输入;printf("给一个整数:");fflush(标准输出);scanf("%d", &input);printf("再试一次:");fflush(标准输出);scanf("%d", &input);printf("你说... %d\n", input);返回0;}

或者,您可以将输出流设置为无缓冲并且根本不需要刷新它:

#include int main() {整数输入;setvbuf(stdout, NULL, _IONBF, 0);printf("给一个整数:");scanf("%d", &input);printf("再试一次:");scanf("%d", &input);printf("你说... %d\n", input);返回0;}

I have a file called get_int.c on a remote Unix system, containing the following:

#include <stdio.h>

int main() {
    int input;

    printf("Give an integer: ");
    fflush(stdout);
    scanf("%d", &input);

    printf("Try again: ");
    scanf("%d", &input);

    printf("You said... %d\n", input);

    return 0;
}

I have a command to compile and run this file from my local WSL:

sshpass -f pass.txt ssh username@remote.host.address "cd path/to/file/ && gcc get_int.c && a.out"

When I execute this command, I successfully get the prompt Give an integer: and provide one and press enter. Then, however, I do not get the prompt Try again:. I can still type an integer (123) and press enter. When I do, it then prints Try again: You said... 123

As you can see, no printing occurs until I either fflush(stdout) or the program ends. Can I possibly modify my ssh command so that output goes to the local terminal without having to fflush before every scanf?

解决方案

Output to stdout does not seem to be flushed when reading from stdin on your system in the specific circumstances described. You must flush stdout explicitly with fflush() before every call to scanf():

#include <stdio.h>

int main() {
    int input;

    printf("Give an integer: ");
    fflush(stdout);
    scanf("%d", &input);

    printf("Try again: ");
    fflush(stdout);
    scanf("%d", &input);

    printf("You said... %d\n", input);

    return 0;
}

Alternately, you can set the output stream as unbuffered and won't need to flush it at all:

#include <stdio.h>

int main() {
    int input;

    setvbuf(stdout, NULL, _IONBF, 0);

    printf("Give an integer: ");
    scanf("%d", &input);

    printf("Try again: ");
    scanf("%d", &input);

    printf("You said... %d\n", input);

    return 0;
}

这篇关于在通过 scanf() 等待输入之前没有通过 SSH 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆