令人尴尬的简单问题,但是我似乎无法打开新文件来使用文件描述符进行写入。我尝试过的每一种变化都会返回-1
。我想念什么?这是使用文件描述符初始化文件的方式,对吗?我找不到其他说明的文档。
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd;
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
fd = open ("/home/CSTCIS/sample.dat", O_WRONLY, mode);
printf("%d\n", fd);
}
perror()打印open: No such file or directory
。
成功完成后,[
open
]函数应打开文件并返回一个非负整数,该整数代表编号最小的未使用文件描述符。否则,应返回-1并设置errno以指示错误。如果函数返回-1,则不能创建或修改任何文件。
要检查open()
语句存在什么问题,只需编写:
perror("open");
在printf()
声明之前。
OP已找到解决方案:
open()
如果O_CREAT
包含标志,则该命令有效。fd = open ("/home/CSTCIS/sample.dat", O_WRONLY | O_CREAT, mode);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句