简体   繁体   English

(Unix / Linux)如何从需要输入文件的另一个C程序执行C程序?

[英](Unix/Linux) How would I execute a C program from another C program that requires an input file?

终端代码

In the picture above, the code can simply be run from the terminal. 在上图中,代码可以简单地从终端运行。 pipeclient and pipeserver files are run. pipeclient和pipeserver文件运行。 pipeclient file takes command.txt as an input using the < symbol. pipeclient文件使用<符号将command.txt作为输入。

Now, if I don't want to run the pipeclient file from the terminal, but want to run from a C program, how would I go about it? 现在,如果我不想从终端运行pipeclient文件,而是想从C程序运行,我将如何处理? Will the exec set of functions help me? exec函数集对我有帮助吗? How would I run the pipeclient file with the command.txt input file from a C program ? 我将如何使用C程序中的command.txt输入文件运行pipeclient文件?

The low-level way to run a program with input taken from a file is to: 使用从文件中获取输入来运行程序的底层方法是:

  • open the file for reading using open 使用open文件进行读取
  • use the dup2 system call to duplicate the open file handle over top of the handle for standard input (always handle 0 ) 使用dup2系统调用将打开文件的句柄复制到用于标准输入的句柄上方(总是句柄0
  • close the old handle (the copy duplicated on top of handle 0 will stay open) close旧手柄(在手柄0顶部重复的副本将保持打开状态)
  • use execve or another function from the exec family to switch to the new program, which will now have its standard input opened to the file. 使用execveexec系列中的另一个函数切换到新程序,该程序现在将在文件中打开其标准输入。

This is the same way the shell itself implements the < file input redirection. 这与外壳程序本身实现< file输入重定向的方式相同。

Here's an example that runs /usr/bin/rev using input from a commands.txt file in the current directory: 这是一个示例,该示例使用当前目录中的commands.txt文件的输入来运行/usr/bin/rev

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

extern char **environ;

#define ERROR(msg) do { perror(msg); exit(1); } while(0)

int main()
{
    // open the input file
    int fd = open("commands.txt", O_RDONLY);
    if (fd == -1)
        ERROR("open");

    // "dup" the handle to standard input (handle 0)
    if (dup2(fd, 0) == -1)
        ERROR("dup2");

    // close the old handle
    close(fd);

    // exec the program
    char *args[] = {"rev", NULL};
    execve("/usr/bin/rev", args, environ);

    // the program never gets here, unless the exec fails
    ERROR("execve");
    return -1;
}

You can also use the system command which executes a shell command including redirection, so the program: 您还可以使用system命令执行包含重定向的shell命令,因此该程序:

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

#define ERROR(msg) do { perror(msg); exit(1); } while(0)

int main()
{
    if (system("/usr/bin/rev <commands.txt"))
        ERROR("system");

    // this *will* return after completion
    return 0;
}

will work as well. 也会工作。 Here, the system call is actually invoking a copy of a shell ("/bin/sh") to process the command line as if it was a shell command. 在这里, system调用实际上是在调用shell的副本(“ / bin / sh”)来处理命令行,就好像它是shell命令一样。

This is more convenient, but you have less control over the process of invoking the child program (eg, setting up its environment, cleaning its argument list, etc.). 这更方便,但是您对调用子程序的过程的控制较少(例如,设置其环境,清除其参数列表等)。 There are also complex potential security issues with using system that can be a problem if your program will run as root , though that may not be an issue here. 如果您的程序将以root身份运行,则使用system还存在一些潜在的复杂安全问题,尽管这在这里可能不是问题。

you can use system function to call your 2nd program for 1st c program.like 您可以使用system功能来调用您的第二个程序为第一个c程序。

1st.c 1st.c

#include <stdio.h>
#include<ctype.h>
#include <stdlib.h>
int main()
{
    printf("I m 1st program");
    system("./2nd.out\n");
}

2nd.c 2nd.c

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


int main(int argc, char **argv)
{
        printf("Hello there i m 2nd program\n")

}

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

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