簡體   English   中英

將C程序從Linux遷移到Windows

[英]Migrating a C program from Linux to Windows

我想用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 <sys/types.h> //Specified in man 2 open
#include <sys/stat.h>    
#include <stdio.h>
    #include <fcntl.h> // open function
    #include <unistd.h> // close function
    #include "colors.h"
    #include "const.h"
    #include <ctype.h>
    #include <errno.h>
    #include <string.h>

我該如何解決這個問題?

對於Windows,您需要包含sys\\stat.h ,並且可用的模式標志是_S_IREAD_S_IWRITE ,如果需要可以組合使用。 文檔可以在這里找到。

特別注意這個評論:

如果為pmode指定了除上述值之外的值(即使它將在另一個操作系統中指定有效的pmode)或指定了允許的oflag值以外的任何值,該函數將在Debug模式下生成一個斷言並調用invalid參數處理程序,如參數驗證中所述。 如果允許繼續執行,則該函數返回-1並將errno設置為EINVAL。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM