c - 创建或打开不起作用。请建议
问题描述
做一些非常简单的事情。运行这个简单的代码来创建一个目录和文件(如果不存在)但不工作。在 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);
解决方案
推荐阅读
- svg - svg blobbed 图像在编码后被破坏
- r - 使用具有相同字符串但顺序不同的列合并两个 data.frame
- c++ - 智能指针或类似于标准 C++ 类的 lock_guard 在析构函数中执行任意代码
- octave - 如何将保存为结构的矩阵转换为矩阵
- angular - Angular 11:延迟加载模块中未识别组件和指令
- c# - 不可为空的引用类型:我可以定义一个默认元素吗?
- mysql - 有没有办法使用 python for mysql (mysqlconnector) 创建表名是用户输入的表?
- firebase - 用于 URL 缩短的 Firebase 扩展:如何将 Firestore 路径放入 URL 的位置?
- java - 当客户端关闭连接而不读取数据时,服务器端的 Java Socket 输出流写入将被永远阻塞。如何处理?
- python - IDLE 在 mac 上使用与 python3 不同的 python 版本