I want to open a file in C with 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've wrote this code for the first time in Linux
, and It was working, but when I run it in 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 that problem?
With Windows you need to include sys\stat.h
, and the mode flags available are _S_IREAD
and _S_IWRITE
, which can be combined if needed. Documentation can be found here.
Note in particular this comment:
If a value other than the above is specified for pmode (even if it would specify a valid pmode in another operating system) or any value other than the allowed oflag values is specified, the function generates an assertion in Debug mode and invokes the invalid parameter handler as described in Parameter Validation. If execution is allowed to continue, the function returns -1 and sets errno to EINVAL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With