Porting C from Linux to Windows

I want to open a file in C using the open () function, and this is the code I use:

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); } 

I wrote this code for the first time on Linux , and it worked, but when I ran it on Windows , I got this error message:

 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)| 

These are the headers I included:

 #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> 

How can I solve this problem?

+6
source share
1 answer

On Windows, you need to enable sys\stat.h , and the mode flags are _S_IREAD and _S_IWRITE , which can be combined if necessary. The documentation can be found here .

Pay particular attention to this comment:

If pmode is set to a value other than the one specified above, even if it sets a valid pmode on another operating system) or any value other than the allowed tolag values ​​is specified, the function generates a statement in debug mode and calls an invalid parameter handler, as described in "Checking parameters. " If execution is allowed to continue, the function returns -1 and sets errno to EINVAL.

+11
source

All Articles