繁体   English   中英

在 C 中,如何向先前使用 system() 调用的程序提供输入?

[英]In C, how do I give an input to a program previously called with system()?

我目前想制作一个用 system() 调用的程序,在输入不是参数的情况下接受输入。 由于可能有点不清楚,我将用一个简单的例子来说明我的问题。

假设我们有 2 个程序, askname.c 和 call.c

问名.c

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
     char name[10];
     printf("What is your name ? ");
     fgets(name, 10, stdin);
     printf("So you are %s \n", name);
 
     return 0;
}

在此之后,我会用 call.c 调用 askname

#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[]){
char* foo ="test";
system("./askname");
//somehow make askname receive foo to display "So you are test"
return 0;
}

知道如何进行吗?

你没有。 你用别的东西。 也许popen

FILE *fp = popen("./askname", "w");
fprintf(fp, "%s\n", foo);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM