简体   繁体   English

如何在C中使用argv重定向给定文件中的stdout和stdin

[英]How to redirect stdout and stdin in a given file using argv in C

I want to redirect stdout and stdin in a specific file which would be given in argv array. 我想在argv数组中给出的特定文件中重定向stdout和stdin。

For instance when I enter a command like - ./shell ls > test 例如,当我输入--/ shell ls> test之类的命令时

it should be redirected to the "test" file, now I am bit confuse because without writing any code it automatically redirects to that file, I want to do it manually, secondly, when I enter a command like- ./shell ls < test, the stdin should be redirected. 应该将其重定向到“测试”文件,现在我有点困惑,因为在不编写任何代码的情况下,它会自动重定向到该文件,因此我想手动进行操作,其次,当我输入类似-./shell ls <test的命令时,则应重定向标准输入。 I tried to find a file name and ">" or "<" sign using argv[argc-1] and argv[argc-2], but it seems that when I use ">" and a filename afterwards, the output prints(the arguments before ">" "<" sing) in that file instead of getting that name and a sign. 我尝试使用argv [argc-1]和argv [argc-2]查找文件名和“>”或“ <”符号,但是似乎当我随后使用“>”和文件名时,输出显示(该文件中的“>”“ <”之前的参数),而不是获取该名称和符号。

Basically, I am creating a shell command using execvp() and fork(). 基本上,我正在使用execvp()和fork()创建一个shell命令。

Here is my code, I am able to redirect stdout in a static file. 这是我的代码,我能够在静态文件中重定向stdout。

void call_system(char *argv[],int argc)
    {
    int pid;
    int status=0;
    signal(SIGCHLD, SIG_IGN);
    int background;
    /*two process are created*/
    pid=fork();
    background = 0;

    if(pid<0)
    {
        fprintf(stderr,"unsuccessful fork /n");
         exit(EXIT_SUCCESS);
    }
    else if(pid==0)
    {
        //system(argv[1]);
        /*argument will be executed*/

                freopen("CON","w",stdout);
                    char *bname;
                    char *path2 = strdup(*argv);
                    bname = basename(path2);


                        execvp(bname, argv);
                         fclose (stdout);
    }
    else if(pid>0)
    {
    /*it will wait untill the child process doesn't finish*/
    //waitpid(pid,&status,0);

    wait(&status);

    //int tempid;
    //tempid=waitpid(pid,&status,WNOHANG);
    //while(tempid!= pid);// no blocking wait


    if(!WIFEXITED(status) || WEXITSTATUS(status))
    printf("error");



                         exit(EXIT_SUCCESS);

    }
    }

Try using dup() or dup2() or dup3() . 尝试使用dup()dup2()dup3()

The dup() system call creates a copy of the file descriptor oldfd, using the lowest-numbered unused descriptor for the new descriptor. dup()系统调用使用新描述符的编号最小的未使用描述符创建文件描述符oldfd的副本。

File *fp=fopen(argv[1],"r");
int fd=fileno(fp);

dup2(fd,0); //dup2(fd,STDIN_FILENO) redirect file stream to input stream
scanf("%s",buff); //reading from file.

Similarly output can also be redirected.From manual these informations may be useful 类似的输出也可以重定向。从手册中这些信息可能会有用

On program startup, the integer file descriptors associated with the
       streams stdin, stdout, and stderr are 0, 1, and 2, respectively.  The
       preprocessor symbols STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO
       are defined with these values in <unistd.h>.

Suppose you want to redirect stdout to this file. 假设您要将标准输出重定向到此文件。

dup2(fd,1);//dup2(fd,STDOUT_FILENO)
printf("%s",buff); //this will write it to the file.

stdio redirection is handled by the shell, not the launched program. stdio重定向由外壳(而不是启动的程序)处理。 The relevant syscalls are pipe , open and dup2 , the later of the two is used to redirect the stdio filedescriptors into the pipe or file to be read from or written to. 相关的系统调用是pipeopendup2 ,两者中的较晚者用于将stdio文件描述符重定向到要读取或写入的管道或文件中。

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

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