将C程序从Linux迁移到Windows

 手机用户2502894247 发布于 2023-02-08 11:03

我想用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 

我该如何解决这个问题?

1 个回答
  • 对于Windows,您需要包含sys\stat.h,并且可用的模式标志是_S_IREAD_S_IWRITE,如果需要,可以组合使用.文档可以在这里找到.

    特别注意这个评论:

    如果为pmode指定了除上述值之外的值(即使它将在另一个操作系统中指定有效的pmode)或指定了允许的oflag值以外的任何值,该函数将在Debug模式下生成一个断言并调用invalid参数处理程序,如参数验证中所述.如果允许继续执行,则该函数返回-1并将errno设置为EINVAL.

    2023-02-08 11:05 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有