我想用open()函数在C中打开一个文件,这是我使用的代码:
int lire(){ char buf[1024]; int bytesRead; int fildes; char path[128]; mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; int flags = O_RDONLY; printf("\n%s-->Donner l'emplacement du fichier :%s ", CYAN_NORMAL, RESETCOLOR); scanf("%s", path); fildes = ouvrir(path, flags, mode); if(fildes == -1){ return 0; } while ((bytesRead = read(fildes, buf, sizeof buf)) > 0) { write(STDOUT_FILENO, buf, bytesRead); } close(fildes); return 1; } int ouvrir(char *path, int flags, mode_t mode) { return open(path, flags, mode); }
我第一次写了这段代码Linux
,它正在工作,但是当我运行它时,Windows
我收到了这条错误消息:
error: 'S_IRUSR' undeclared (first use in this function)| error: 'S_IWUSR' undeclared (first use in this function)| error: 'S_IRGRP' undeclared (first use in this function)| error: 'S_IROTH' undeclared (first use in this function)|
这些是我包含的标题:
#include//Specified in man 2 open #include #include #include // open function #include // close function #include "colors.h" #include "const.h" #include #include #include
我该如何解决这个问题?
对于Windows,您需要包含sys\stat.h
,并且可用的模式标志是_S_IREAD
和_S_IWRITE
,如果需要,可以组合使用.文档可以在这里找到.
特别注意这个评论:
如果为pmode指定了除上述值之外的值(即使它将在另一个操作系统中指定有效的pmode)或指定了允许的oflag值以外的任何值,该函数将在Debug模式下生成一个断言并调用invalid参数处理程序,如参数验证中所述.如果允许继续执行,则该函数返回-1并将errno设置为EINVAL.