如何将文件内容重定向到标准输入,为什么exec函数不起作用? [英] how to redirect content of file to stdin, why the exec function doesn't work?

查看:75
本文介绍了如何将文件内容重定向到标准输入,为什么exec函数不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将input.txt文件的内容重定向到stdin.

I want to redirect a content of input.txt file to stdin.

我尝试过:

int RunCompiledFile(char *path, char * input, char *thirdLine, char* name){

    int fdFile = open(input, O_RDONLY), exitStatus, check,
    fdFileOutput = open("newOutput.txt", O_RDWR|O_CREAT, 0777), status;
    char* args[] = {"comp.out", "newOutput.txt", thirdLine};
    char buffer[100];
    pid_t  pid;


    dup2(0, fdFile);
    dup2(fdFileOutput , 1);
    getcwd(buffer, 95);
    i = strlen(buffer);
    buffer[i] = '/';
    buffer[++i] = '\0';
    check = execv(buffer,"a.out");
    close(fdFileOutput);

    //if ((pid = fork()) == 0){
        check = execvp("comp.out", args);
      //  return 5;}

    wait(&status);
    exitStatus = WEXITSTATUS(status);

    switch (exitStatus){
        case 1:
            result("0", name, BADOUTPUT);
            unlink("newOutput.txt");
            return 1;
        case 2:
            if (depth){
                char buffer[5];
                sprintf(buffer, "%d", 100 - (10 * depth));
                result(buffer, name, WRONGDIRECTORY);}
            else
                result("100", name, GREATJOB);
                unlink("newOutput.txt");
            return 1;
        case 3:
            result("80", name, SIMILLAROUTPUT);
            unlink("newOutput.txt");
            return 1;
    }

    return fdFileOutput;
}

但是由于所有分支,很难调试.execv-在"getcwd"返回-1之后.a.out是一个程序,它通过scanf()获取输入,然后执行某些操作并打印出答案.

But becuase of all the forks it's hard to debbug. the execv - after the "getcwd" returns -1. a.out is a program the gets input via scanf(), and then does something and printf the answer.

我应该得到一个文件,将其转换为stdin以获取a.out的fget,然后打开一个新文件以输出a.out.

I am supposed to get a file, turn it to stdin for the fgets of a.out, and then open a new file for the output of a.out.

它仍然不起作用,我尝试了您的修复程序:

it's still doesn't work, I tried your fixes:

    dup2(fdFile, STDIN_FILENO);
    dup2(fdFileOutput, STDOUT_FILENO);
    getcwd(buffer, 95);
    i = strlen(buffer);
    buffer[i] = '/';
    buffer[++i] = 'a';
    buffer[++i] = '.';
    buffer[++i] = 'o';
    buffer[++i] = 'u';
    buffer[++i] = 't';
    buffer[++i] = '\0';
    close(fdFileOutput);
    close(fdFile);
    check = execv(buffer,"a.out");

,但是execv的返回值仍为-1.a.out在不同的用户上工作(我的朋友以前尝试过).

but the return from execv is still -1. the a.out is working on different users (my friends tried it before).

推荐答案

来自 正确的方法是替换文件描述符 STDIN_FILENO STDOUT_FILENO STDERR_FILENO ,并使用 dup2()打开文件.您还应该在子进程中关闭原始文件:

The right way to do it is to replace the file descriptors STDIN_FILENO, STDOUT_FILENO and STDERR_FILENO with the opened files using dup2(). You should also then close the original files in the child process:

else if (pid == 0)
{
    dup2(fileno(someopenfile), STDIN_FILENO);
    dup2(fileno(someotherfile), STDOUT_FILENO);
    dup2(fileno(somethirdopenfile), STDERR_FILENO);
    fclose(someopenfile);
    fclose(someotheropenfile);
    fclose(somethirdopenfile);
    execvp(args[0], args);
    // handle error ...
}

因此打开输入和输出文件,创建子进程( fork()),然后创建 dup2()和上面的其余代码

So open input and output files, create the childprocess (fork()) and then dup2() and rest of the code above

除此之外,您还可以使用 freopen()(重新路由C 中的stdin和stdout)

Beside this you can use freopen() (Rerouting stdin and stdout from C)

freopen("newin","r",stdin); (未分配文件句柄[!])

freopen("newin", "r", stdin); (without assignment to file handle [!])

这些链接也很有趣,因为它们提供有关系统功能的信息,例如 fork() dup2(),...

These links are also interesting because they give information about the system functions like fork(), dup2(),...

如何使用C语言执行Shell脚本Linux?

Bash:从文件读取stdin并写入stdout提交

这篇关于如何将文件内容重定向到标准输入,为什么exec函数不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆