如何将程序输出重定向到文本文件

时间:2016-01-20 19:17:36

标签: c linux file

我想将程序的输出重定向到文件。我怎样才能做到这一点?目前我的文件没有创建,我只能将输出打印到我的控制台。

    int fd[2];
    int processId;
    int output;
    char filename[] = "output.txt";

    if((output = open(filename, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR)) == -1){
        fprintf(stderr, "Unable to create/open file '%s'\n", filename);
        return 1;
    }

    if(pipe(fd) == -1){
        fprintf(stderr, "Error creating pipe\n");
        return 2;
    }

    if((processId = fork()) == -1){
        fprintf(stderr, "Error forking\n");
        return 3;
    }   

    if(processId == 0){
        int newFD = dup(STDOUT_FILENO);
        char newFileDescriptor[2];
        sprintf(newFileDescriptor, "%d", newFD);
        dup2(fd[1], output);
        close(fd[0]);
        execl("./pipetest", "pipetest", newFileDescriptor, NULL);
    }else{
        close(fd[1]);
        char c[10];
        int r = read(fd[0], c, sizeof(char) * 10);

        if(r > 0){
            fprintf(stderr, "PIPE INPUT = %s", c);
            fwrite(c, 1, sizeof(c), output);
        }
    }

4 个答案:

答案 0 :(得分:4)

良好的开端并不是忽略编译器警告:

test.c: In function ‘main’:
test.c:42:13: warning: passing argument 4 of ‘fwrite’ makes pointer from integer without a cast [enabled by default]
             fwrite(c, 1, sizeof(c), output);
             ^
In file included from test.c:1:0:
/usr/include/stdio.h:715:15: note: expected ‘struct FILE * __restrict__’ but argument is of type ‘int’
 extern size_t fwrite (const void *__restrict __ptr, size_t __size,
               ^

intFILE*不可互换。如果您使用open,请使用write进行书写。如果您使用fopen,请使用fwrite

此外,您永远不会修改进程的标准输出。而是修改output,这是没有意义的。以下是使代码工作的最小更改:

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

int main() {                                                                                                                                                                                                                                                                                                                                                                                                                             
    int fd[2];                                                                                                                                                                                                                                                                                                                                                                                                                           
    int processId;                                                                                                                                                                                                                                                                                                                                                                                                                       
    int output;                                                                                                                                                                                                                                                                                                                                                                                                                          
    char filename[] = "output.txt";                                                                                                                                                                                                                                                                                                                                                                                                      

    if((output = open(filename, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR)) == -1){                                                                                                                                                                                                                                                                                                                                                          
        fprintf(stderr, "Unable to create/open file '%s'\n", filename);                                                                                                                                                                                                                                                                                                                                                                  
        return 1;                                                                                                                                                                                                                                                                                                                                                                                                                        
    }                                                                                                                                                                                                                                                                                                                                                                                                                                    

    if(pipe(fd) == -1){                                                                                                                                                                                                                                                                                                                                                                                                                  
        fprintf(stderr, "Error creating pipe\n");                                                                                                                                                                                                                                                                                                                                                                                        
        return 2;                                                                                                                                                                                                                                                                                                                                                                                                                        
    }                                                                                                                                                                                                                                                                                                                                                                                                                                    

    if((processId = fork()) == -1){                                                                                                                                                                                                                                                                                                                                                                                                      
        fprintf(stderr, "Error forking\n");                                                                                                                                                                                                                                                                                                                                                                                              
        return 3;                                                                                                                                                                                                                                                                                                                                                                                                                        
    }                                                                                                                                                                                                                                                                                                                                                                                                                                    

    if(processId == 0){                                                                                                                                                                                                                                                                                                                                                                                                                  
        int newFD = dup(STDOUT_FILENO);                                                                                                                                                                                                                                                                                                                                                                                                  
        char newFileDescriptor[2];                                                                                                                                                                                                                                                                                                                                                                                                       
        sprintf(newFileDescriptor, "%d", newFD);                                                                                                                                                                                                                                                                                                                                                                                         
        dup2(fd[1], STDOUT_FILENO); // You want to modify STDOUT_FILENO                                                                                                                                                                                                                                                                                                                                                                  
        close(fd[0]);                                                                                                                                                                                                                                                                                                                                                                                                                    
        execl("/bin/echo", "echo", newFileDescriptor, NULL); // switched to echo                                                                                                                                                                                                                                                                                                                                                         
    }else{                                                                                                                                                                                                                                                                                                                                                                                                                               
        close(fd[1]);                                                                                                                                                                                                                                                                                                                                                                                                                    
        char c[10];                                                                                                                                                                                                                                                                                                                                                                                                                      
        int r = read(fd[0], c, sizeof(char) * 10);                                                                                                                                                                                                                                                                                                                                                                                       

        if(r > 0){                                                                                                                                                                                                                                                                                                                                                                                                                       
            fprintf(stderr, "PIPE INPUT = %s", c);                                                                                                                                                                                                                                                                                                                                                                                       
            write(output, c, r); // use write instead of fwrite                                                                                                                                                                                                                                                                                                                                                                          
        }                                                                                                                                                                                                                                                                                                                                                                                                                                
    }                                                                                                                                                                                                                                                                                                                                                                                                                                    
}                                                                                                                                                                                                                                                                                                                                                                                                                                        

以下是运行它的结果:

$ gcc test.c -o test
$ ./test
PIPE INPUT = 6
$ cat output.txt 
6

答案 1 :(得分:1)

最简单的方法是通过调用:

在bash中执行此操作
./myprogram > output.txt

这会将所有输出重定向到output.txt

答案 2 :(得分:1)

您可以使用freopen,这可能是最简单的方法:

if (!freopen("out.txt", "w", stdout))
{
    // failed to open the file stream, handle the error
}

然后,您可以使用printf写入该文件。您可以对stderr执行相同的操作,但这可能是一个坏主意 - 根据定义stderr用于报告错误,并且从我所知的惯例是将其输出到控制台

答案 3 :(得分:0)

以下简单示例如何执行此操作:

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

int main(void) {
    int out = open("/tmp/output.txt", O_WRONLY | O_CREAT, 0644);
    if (out == -1) {
        perror("open:");
        return -1;
    }
    int r;
    r = close(1); /* this closes stdout */
    if (r != 0) {
        perror("close:");
        return -1;
    }
    r = dup2(out, 1); /* this duplicates your file descriptor to stdout */
    if (r == -1) {
        perror("dup2:");
        return -1;
    }
    printf("this should go to the output file\n");
    return 0;
}