不写入指定文件

时间:2021-02-16 01:19:18

I am trying to redirect output of stdout and stderr using a file. I am using freopen and it creates the file in the correct directory but the file is blank. When I comment out the code to redirect the stdout and stderr - the output shows up on the console.

我正在尝试使用一个文件重定向stdout和stderr的输出。我正在使用freopen,它在正确的目录中创建文件,但是文件是空的。当我注释掉要重定向stdout和stderr的代码时——输出显示在控制台。

Here is the code:

这是代码:

freopen(stderrStr.c_str(), "a+", stderr); //where stderrStr and stdoutStr are the path/file name
freopen(stdoutStr.c_str(), "a+", stdout);

fclose(stdout); 
fclose(stderr);

I have placed a printf("I WORK") in main and without the the suppressant it outputs but wont write to the file.

我已经将printf(“I WORK”)放在主文件中,没有它输出的抑制因子,但不会写入文件。

1 个解决方案

#1


3  

In order to do what you are trying to do I use dup2(2).

为了做你想做的,我用了dup2(2)

Simply open(2) two files fd1 and fd2 and then use dup2(fd1, 1) for stdout and dup2(fd2, 2) for stderr.

只需打开(2)两个文件fd1和fd2,然后使用dup2(fd1, 1)作为stdout,使用dup2(fd2, 2)作为stderr。

The OS (libc, loader or kernel, not sure which) sets up 3 open file descriptors before the entry of main:

操作系统(libc,加载器或内核,不确定哪一个)在main的进入之前设置3个打开的文件描述符:

0 : stdin pipe
1 : stdout pipe
2 : stderr pipe

and from the docs of dup2:

来自dup2的文档:

dup2(int oldfd, int newfd) makes newfd be the copy of oldfd, closing newfd first if necessary

dup2(int oldfd, int newfd)使newfd为oldfd的拷贝,必要时先关闭newfd

so the two dup2 calls replace 1 and 2 with your open files. So after when your process calls write(2) (the syscall all output routines such as printf and cout call) to fd 1 or fd 2 the data will be written to your files rather than to the pipes setup by the OS

因此,两个dup2调用用打开的文件替换1和2。因此,当您的进程调用write(2) (syscall所有输出例程,如printf和cout调用)到fd 1或fd 2时,数据将被写入到您的文件,而不是由操作系统设置的管道。

man page note:

手册页注意事项:

man pages come in chapters. the notation foo(N) means the man page of name "foo" in chapter N. To open foo(N) type:

手册页分为几章。符号foo(N)表示第N章名称“foo”的man页,以打开foo(N)类型:

$ man N foo

for example to open write(2) type:

例如打开写(2)类型:

$ man 2 write

#1


3  

In order to do what you are trying to do I use dup2(2).

为了做你想做的,我用了dup2(2)

Simply open(2) two files fd1 and fd2 and then use dup2(fd1, 1) for stdout and dup2(fd2, 2) for stderr.

只需打开(2)两个文件fd1和fd2,然后使用dup2(fd1, 1)作为stdout,使用dup2(fd2, 2)作为stderr。

The OS (libc, loader or kernel, not sure which) sets up 3 open file descriptors before the entry of main:

操作系统(libc,加载器或内核,不确定哪一个)在main的进入之前设置3个打开的文件描述符:

0 : stdin pipe
1 : stdout pipe
2 : stderr pipe

and from the docs of dup2:

来自dup2的文档:

dup2(int oldfd, int newfd) makes newfd be the copy of oldfd, closing newfd first if necessary

dup2(int oldfd, int newfd)使newfd为oldfd的拷贝,必要时先关闭newfd

so the two dup2 calls replace 1 and 2 with your open files. So after when your process calls write(2) (the syscall all output routines such as printf and cout call) to fd 1 or fd 2 the data will be written to your files rather than to the pipes setup by the OS

因此,两个dup2调用用打开的文件替换1和2。因此,当您的进程调用write(2) (syscall所有输出例程,如printf和cout调用)到fd 1或fd 2时,数据将被写入到您的文件,而不是由操作系统设置的管道。

man page note:

手册页注意事项:

man pages come in chapters. the notation foo(N) means the man page of name "foo" in chapter N. To open foo(N) type:

手册页分为几章。符号foo(N)表示第N章名称“foo”的man页,以打开foo(N)类型:

$ man N foo

for example to open write(2) type:

例如打开写(2)类型:

$ man 2 write