gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:47:14 24 4
gpt4 key购买 nike

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

    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);
}
}

最佳答案

一个好的开始是不要忽略编译器警告:

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

关于c - 如何将程序输出重定向到文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34908588/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com