我想使用open_excl(3)打开一个独占写入的文件。
我写道:
#include <open.h>
int main(int c, char* v[]){
int fp = open_excl("my_file");
return 0;
}现在: gcc -Wall file.c -o out.a
我得到了一个致命的编译器错误: open.h:没有这样的文件或目录
怎么会这样?我是否遇到了路径中断的问题?缺少指向库的链接?错误版本的gcc?我用的是5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)
发布于 2017-04-20 20:06:05
open_excl不是一个标准函数;我的Linux系统上没有open.h。正如documentation on linux.die.net所说:
open_excl打开文件filename进行写入,并返回文件句柄。在调用open_excl之前,文件可能不存在。该文件将以0600模式创建。
... open_excl依赖于O_EXCL标志来打开...
因此,您可以使用以下命令实现相同的功能
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags, mode_t mode);按如下方式调用它:
int fd = open(filename, O_EXCL|O_CREAT|O_WRONLY, 0600);要将文件描述符包装到FILE *中,请使用fdopen函数:
#include <stdio.h>
FILE *fp = fdopen(fd);发布于 2017-04-20 19:50:48
无论何时包含任何头文件,如果使用尖括号#include <open.h>,编译器将在标准目录中查找相同的文件,如果使用引号#include "open.h",编译器将在项目目录中查找相同的头文件。
因此,您可以首先检查是否在标准目录中有open.h文件(可能不是这样),您可以下载并复制本地目录中的头文件,并使用引号包含相同的头文件。
https://stackoverflow.com/questions/43517246
复制相似问题