你如何产卵用C另一个进程? [英] How do you spawn another process in C?
本文介绍了你如何产卵用C另一个进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您如何运行外部程序,并使用C传递命令行参数?如果你必须使用操作系统的API,包括适用于Windows,Mac和Linux的解决方案。
How do you run an external program and pass it command line parameters using C? If you have to use operating system API, include a solution for Windows, Mac, and Linux.
推荐答案
如果要执行更复杂的操作,例如读取外部程序的输出,你可以更好地通过的popen 系统调用。例如,以编程方式访问目录列表(这是一个有点傻的例子,但有用的为的一个例子),你可以这样写:
If you want to perform more complicated operations, like reading the output of the external program, you may be better served by the popen system call. For example, to programmatically access a directory listing (this is a somewhat silly example, but useful as an example), you could write something like this:
#include <stdio.h>
int main()
{
int entry = 1;
char line[200];
FILE* output = popen("/usr/bin/ls -1 /usr/man", "r");
while ( fgets(line, 199, output) )
{
printf("%5d: %s", entry++, line);
}
}
给这样的输出
1: cat1
2: cat1b
3: cat1c
4: cat1f
5: cat1m
6: cat1s
...
这篇关于你如何产卵用C另一个进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文