【发布时间】:2016-10-12 03:54:34
【问题描述】:
在下面的示例中,我们关闭默认 stderr 并通过 fdopen() 在临时文件上重新打开它,使用描述符 2,这是从临时文件描述符 dup()'ed。然后我们直接write()这个描述符2。我们可以安全地执行此操作,因为这是对文件的第一次写入操作,因此它具有空缓冲区。在此之后,我们fprintf() 到新的stderr。然后我们关闭stderr(因此,它的关联描述符2被自动关闭)。原始描述符fd 仍然有效。通过它,我们转到临时文件的开头,读取其内容并将它们打印到标准输出。但是输出是乱码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
int main(void)
{
int fd;
char buf[200];
int n;
char fname[] = "/tmp/tst-perror.XXXXXX";
fd = mkstemp (fname);
fclose (stderr);
dup2 (fd, 2);
stderr = fdopen (2, "w");
fd = fileno(stderr);
char *s = "this is a test\n";
n = write(fd, s, strlen(s));
fprintf(stderr, "multibyte string\n");
fclose (stderr);
// close(fd);
// fd = open(fname, O_RDONLY);
lseek (fd, 0, SEEK_SET);
n = read (fd, buf, sizeof (buf));
printf("%.*s", (int) n, buf);
close (fd);
return 0;
}
输出是:
$ ./a.out
����
如果我们取消注释“close”和“open”行并注释“lseek”行,输出如预期:
$ ./a.out
this is a test
multibyte string
write() 没有缓冲区,stderr 在关闭时被注销,所以
如果我们在读取文件之前不关闭文件,为什么输出会出现乱码?
【问题讨论】:
-
另外,请注意
lseek在哪个流上。请参阅man 2 lseek中的注释,“某些设备无法搜索,POSIX 未指定哪些设备必须支持 lseek()。”.