如何在 C 中将字符串传送到 popen() 命令中?

Ash*_*Ash 5 c unix md5 popen

我在uni中使用c(或多或少是第一次)工作,我需要从字符数组生成MD5。该分配指定这必须通过创建管道并md5在系统上执行命令来完成。

我已经走到这一步了:

FILE *in;
extern FILE * popen();
char buff[512];

/* popen creates a pipe so we can read the output
 * of the program we are invoking */
char command[260] = "md5 ";
strcat(command, (char*) file->name);
if (!(in = popen(command, "r"))) {
    printf("ERROR: failed to open pipe\n");
    end(EXIT_FAILURE);
}
Run Code Online (Sandbox Code Playgroud)

现在这工作得很好(对于作业的另一部分,需要获取文件的 MD5),但我无法弄清楚如何将字符串通过管道传输到其中。

如果我理解正确的话,我需要做类似的事情:

FILE * file = popen("/bin/cat", "w");
fwrite("hello", 5, file);
pclose(file);
Run Code Online (Sandbox Code Playgroud)

我认为,它将执行 cat,并通过 StdIn 将“hello”传递给它。这是正确的吗?

Jon*_*ler 2

如果您需要将字符串输入到md5程序中,那么您需要知道您的md5程序使用哪些选项。