【发布时间】:2015-02-23 00:23:22
【问题描述】:
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <fcntl.h> // open
#include <stdio.h>
int main() {
close(1); // close standard out
open("log.txt", O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
puts("Captain's log");
chdir("/usr/include");
execl("/bin/ls", "ls", ".", (char *)NULL);
perror("exec failed");
return 0;
}
当我检查 log.txt 时,我找不到“船长的日志”。我认为它在 execl 之前运行,因此它应该在那里!
【问题讨论】:
-
试试
fflush(stdout); -
@kww 问题解决了吗?
-
@immibis 当我添加 'fflush(stdout);'在 'puts("Captain's log");' 之后,它起作用了。谢谢!