在 C++ 中通过 exec 启动的应用程序在终端上处理用户输入
Handing user input on terminal for application launched via exec in C++
如果 /p 没有作为命令行参数提供,xfreerdp 会要求输入密码;通过终端启动时。
但是用execvp或者exec启动的时候没有提示?
如何显示这个提示?有没有一种方法可以让我以编程方式直接在提示符下输入密码?
使用 swift 使用任务和管道在 Mac 中自动处理相同的内容。如何在 C++ 中实现。
Is there a way where I can directly input password on prompt programmatically?
使用popen()
...
的示例(写在C
中)
#include <stdio.h>
#include <stdlib.h>
int main(void) {
const char *cmd = "xfreerdp";
char output[128] = {'[=10=]'};
const char *arg = "myargs";
// Open process
FILE *fp = popen(cmd, "w");
if (!fp) {
fprintf(stderr, "Could not execute command ...\n");
exit(EXIT_FAILURE);
}
// Pass arguments
if (fprintf(fp, "%s", arg) < 0) {
puts("Could not pass arguments ...");
}
// Print command output (if required)
while (fgets(output, sizeof(output), fp) != NULL) {
puts(output);
}
pclose(fp);
return 0;
}
如果 /p 没有作为命令行参数提供,xfreerdp 会要求输入密码;通过终端启动时。
但是用execvp或者exec启动的时候没有提示?
如何显示这个提示?有没有一种方法可以让我以编程方式直接在提示符下输入密码?
使用 swift 使用任务和管道在 Mac 中自动处理相同的内容。如何在 C++ 中实现。
Is there a way where I can directly input password on prompt programmatically?
使用popen()
...
C
中)
#include <stdio.h>
#include <stdlib.h>
int main(void) {
const char *cmd = "xfreerdp";
char output[128] = {'[=10=]'};
const char *arg = "myargs";
// Open process
FILE *fp = popen(cmd, "w");
if (!fp) {
fprintf(stderr, "Could not execute command ...\n");
exit(EXIT_FAILURE);
}
// Pass arguments
if (fprintf(fp, "%s", arg) < 0) {
puts("Could not pass arguments ...");
}
// Print command output (if required)
while (fgets(output, sizeof(output), fp) != NULL) {
puts(output);
}
pclose(fp);
return 0;
}