首页 > 解决方案 > 创建或打开不起作用。请建议

问题描述

做一些非常简单的事情。运行这个简单的代码来创建一个目录和文件(如果不存在)但不工作。在 chmod 0777 中运行。尝试将输出重定向到文件。尝试这个命令。

ls 测试/来源>/tmp/temp.txt

在程序下面创建。我只用它工作的文件名运行它,但没有前缀/tmp。

 char *filename = "/tmp/temp.txt";
 printf("A %d\n",getpid());
 int fd;
 if((fd = open(filename,O_RDWR|O_CREAT|O_APPEND,0777)) < 0)
  printf("Error:%s",strerror(errno));
 
 pid_t child = fork(); 
 if(child == 0){  
 dup2(fd,STDOUT_FILENO);
 }
 printf("B %d\n",getpid());
 close(fd);

标签: cio-redirection

解决方案


推荐阅读