使用popen使用两个可执行文件在C中读写
我知道popen不允许同时读写 为了解决这个问题,我创建了两个文件,1.c用于编写,2.c用于读取。这些文件包括在下面 当我运行1.out时,我在stdout上获得了预期的输出: 但是,2.out在标准输出上不提供任何输出: 我哪里做错了 1.c: 2.c: 您不想读取标准输出,而是:使用popen使用两个可执行文件在C中读写,c,linux,ubuntu,ubuntu-18.04,popen,C,Linux,Ubuntu,Ubuntu 18.04,Popen,我知道popen不允许同时读写 为了解决这个问题,我创建了两个文件,1.c用于编写,2.c用于读取。这些文件包括在下面 当我运行1.out时,我在stdout上获得了预期的输出: 但是,2.out在标准输出上不提供任何输出: 我哪里做错了 1.c: 2.c: 您不想读取标准输出,而是: while (fgets(path, sizeof(path), fp) != NULL) { bodhi@bodhipc:~/Downloads$ ./2.out bodhi@bodhipc:~/Down
while (fgets(path, sizeof(path), fp) != NULL) {
bodhi@bodhipc:~/Downloads$ ./2.out
bodhi@bodhipc:~/Downloads$
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char *argv[] )
{
FILE *fp;
char path[1035];
/* Open the command for writing. */
fp = popen("./stockfish", "w");
if (fp == NULL) {
printf("Failed to run command\n" );
exit(1);
}
fprintf(fp,"uci\n");
/* close */
pclose(fp);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char *argv[] )
{
FILE *fp;
char path[1035];
/* Open the command for reading. */
fp = popen("./1.out", "r");
if (fp == NULL) {
printf("Failed to run command\n" );
exit(1);
}
/* Read the output a line at a time - output it.*/
while (fgets(path, sizeof(path), stdout) != NULL) {
printf("%s", path);
printf("Done!\n");
}
/* close */
pclose(fp);
return 0;
}
while (fgets(path, sizeof(path), stdout) != NULL) {
while (fgets(path, sizeof(path), fp) != NULL) {