Linux 将输出重定向到 C 中的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8516823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:39:50  来源:igfitidea点击:

redirecting output to a file in C

linuxshell

提问by Zeemaan

i coded a basic shell in C for executing basic commands it'll execute commands ls, ls -al, ls -al | moreetc.

我C编码的一个基本的壳,用于执行基本命令它会执行命令lsls -alls -al | more等。

i want to execute following command in my shell. like ;

我想在我的 shell 中执行以下命令。喜欢 ;

ls -al > a.txt

this will give me a.txtfile which contain output of ls -alprocess. i found a one solution, it's changing the command in my shell like [command1] | tee [filename]. in this instance it'll change ls -al > a.txtto ls -al | tee a.txt. but this process gives the output to file and terminal as well. how to stop printing output in terminal.

这会给我a.txt包含ls -al进程输出的文件。我找到了一个解决方案,它正在更改我的 shell 中的命令,例如[command1] | tee [filename]. 在这种情况下,它将更改ls -al > a.txtls -al | tee a.txt. 但是这个过程也将输出提供给文件和终端。如何在终端中停止打印输出。

or are there are any better solution rather than using tee command. thanks in advance...

或者有没有比使用 tee 命令更好的解决方案。提前致谢...

采纳答案by sehe

This is the result of my testing things out with dup2

这是我用 dup2 测试的结果

The more subtle point is remembering fflush at the right times :) Otherwise, you'll get very surprising results.

更微妙的一点是在正确的时间记住 fflush :) 否则,你会得到非常令人惊讶的结果。

Also, prefer filenoinstead of hardcoding 1(stdout) 2(stderr).

此外,更喜欢fileno而不是硬编码1(stdout) 2(stderr)。

Redirecting stdinwas left as an exercise for the reader

重定向stdin留给读者作为练习

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

int main(int argc, const char *argv[])
{
    int out = open("cout.log", O_RDWR|O_CREAT|O_APPEND, 0600);
    if (-1 == out) { perror("opening cout.log"); return 255; }

    int err = open("cerr.log", O_RDWR|O_CREAT|O_APPEND, 0600);
    if (-1 == err) { perror("opening cerr.log"); return 255; }

    int save_out = dup(fileno(stdout));
    int save_err = dup(fileno(stderr));

    if (-1 == dup2(out, fileno(stdout))) { perror("cannot redirect stdout"); return 255; }
    if (-1 == dup2(err, fileno(stderr))) { perror("cannot redirect stderr"); return 255; }

    puts("doing an ls or something now");

    fflush(stdout); close(out);
    fflush(stderr); close(err);

    dup2(save_out, fileno(stdout));
    dup2(save_err, fileno(stderr));

    close(save_out);
    close(save_err);

    puts("back to normal output");

    return 0;
}

回答by Blagovest Buyukliev

Before you call execve(2)in the newly created process to execute the command, you can redirect its standard input or output via the dup2(2)system call:

在调用execve(2)新创建的进程执行命令之前,您可以通过dup2(2)系统调用重定向其标准输入或输出:

/* redirect stdout to a file */
dup2(1, some_open_file_descriptor);

Of course, you need to have some error handling.

当然,您需要进行一些错误处理。

回答by Jonathan Leffler

Don't use pipe when the output is to go to a file.

当输出到文件时不要使用管道。

When you fork the child to run the lscommand, you note the redirection, and open the file; you then use dup2()(or close()and dup()) so that the file descriptor is now standard output for the child; you close the duplicated file descriptor - the one returned by open(); then you execute lsas usual; its standard output is now sent to the file.

当你 fork 子进程运行ls命令时,你注意到重定向,并打开文件;然后您使用dup2()(or close()and dup()) 以便文件描述符现在是孩子的标准输出;您关闭重复的文件描述符 - 返回的那个open();然后你ls照常执行;它的标准输出现在被发送到文件。

Note that you do the non-pipe I/O redirection after forking, not before. Pipes have to be set up before forking, but other I/O redirection does not.

请注意,您在分叉之后而不是之前进行非管道 I/O 重定向。必须在分叉之前设置管道,但其他 I/O 重定向则不需要。