Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C中使用execvp()对文件进行排序并写入另一个文件_C_Linux_Unix_Exec_Execvp - Fatal编程技术网

如何在C中使用execvp()对文件进行排序并写入另一个文件

如何在C中使用execvp()对文件进行排序并写入另一个文件,c,linux,unix,exec,execvp,C,Linux,Unix,Exec,Execvp,假设我的主目录中有temp.txt,我想对该文件中的所有数据进行排序,并将所有排序后的数据写入另一个名为hello.txt的文件。这是我尝试过的代码编程c: #include <stdio.h> #include <stdlib.h> int main(int agrc,char *argv[]){ char *argv1[]={"sort","temp.txt",">", "hello.txt",NULL}; printf("hello I wi

假设我的主目录中有temp.txt,我想对该文件中的所有数据进行排序,并将所有排序后的数据写入另一个名为hello.txt的文件。这是我尝试过的代码编程c:

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

int main(int agrc,char *argv[]){
    char *argv1[]={"sort","temp.txt",">", "hello.txt",NULL};
    printf("hello I will sort a file\n");

    execvp(argv1[0],argv1);



}
有人能告诉我我的代码出了什么问题吗?有人能告诉我怎么修吗?谢谢你的帮助

在shell中键入sort temp.txt>hello.txt时,不会将>和hello.txt作为参数传递给sort。但当您如上所述调用execvp时,您将这些作为参数传递给sort。如果希望shell将>视为重定向运算符,则需要将字符串传递给sh并让它对其求值:

char *argv1[]={ "sh", "-c", "sort temp.txt > hello.txt", NULL };
“正确的”方法是通过复制文件描述符自己进行重定向。为了清晰起见,省略了一些类似于错误检查的内容:

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


int
main( int argc, char **argv )
{
    int fd;
    const char *input = argc > 1 ? argv[1] : "temp.txt";
    const char *output = argc > 2 ? argv[2] : "hello.txt";
    char * argv1[] = { "sort", input, NULL };

    fd = open( output, O_WRONLY | O_CREAT, 0777 );
    if( fd == -1 ) {
        perror(output);
        return EXIT_FAILURE;
    }

    printf("hello I will sort a file\n");
    fclose(stdout);
    dup2( fd, STDOUT_FILENO);
    close(fd);
    execvp(argv1[0],argv1);
}

谢谢,这很有帮助!
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>


int
main( int argc, char **argv )
{
    int fd;
    const char *input = argc > 1 ? argv[1] : "temp.txt";
    const char *output = argc > 2 ? argv[2] : "hello.txt";
    char * argv1[] = { "sort", input, NULL };

    fd = open( output, O_WRONLY | O_CREAT, 0777 );
    if( fd == -1 ) {
        perror(output);
        return EXIT_FAILURE;
    }

    printf("hello I will sort a file\n");
    fclose(stdout);
    dup2( fd, STDOUT_FILENO);
    close(fd);
    execvp(argv1[0],argv1);
}