Migrating a C program from Linux to Windows - c

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.

Related

programm can not find constant in libary fcntl.h in c

I am learning C and somehow my programm can not find a constant defined in a libary. In my understanding S_IRUSR|S_IWUSR shoud be defined in fcntl.h, but I get while trying to compile this error:
... error: 'S_IRUSR' undeclared (first use in this function)
... error: 'S_IWUSR' undeclared (first use in this function)
My programm looks like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char *argv[]){
int filedeskriptor;
char SchreibeTxt [100] = "Hallo getMonth", LeseTxt [100];
filedeskriptor = open("getMonthTxt", O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
if (filedeskriptor == -1){
printf("Fehler beim Öffnen von mydat \n");
exit (EXIT_FAILURE);
}
if (write(filedeskriptor, SchreibeTxt, sizeof(SchreibeTxt)) == -1){
printf("Fehler beim Schreiben in mydat \n");
exit (EXIT_FAILURE);
}
printf("In getMonthTxt geschrieben: %s \n", SchreibeTxt);
close(filedeskriptor);
return EXIT_SUCCESS;
}
Any help?
Thanks
It depends on according to which POSIX-version your compiler and implementation is build up to, because S_IRUSR and S_IWUSR are only provided inside of fcntl.h in POSIX.1-2008 as Ian Abbott said in the comments.
If your compiler uses a preceding POSIX-version, the macros S_IRUSR and S_IWUSR are not defined in fcntl.h as you can see here. They are then defined in the header sys/stat.h.
Here is a link to the description about the content of the header sys/stat.h, where you can find those:
https://pubs.opengroup.org/onlinepubs/007908799/xsh/sysstat.h.html
So if your compiler uses a version predating POSIX.1-2008, add #include <sys/stat.h> at the top of your code or otherwise if you don´t need anything from fcntl.h replace it with that.
Maybe you're missing the headers
<sys/types.h>
<sys/stat.h>
man 2 open states 3 headers for open()

Error message when trying to use Open Function call

As part of my homework, I need to create a small c program for XV6. This program has to create a file, write to it, then close it.
When I try and compile the xv6 with my file I am receiving two error message.
HWC2.c:13:82: error: ‘O_CREAT’ undeclared (first use in this function)
fd = open("/home/kyle/Desktop/Projects/'Assignment 2'/xv6/Tom.txt", O_RDWR | O_CREAT);
^
HWC2.c:13:14: error: called object ‘open’ is not a function or function pointer
fd = open("/home/kyle/Desktop/Projects/'Assignment 2'/xv6/Tom.txt", O_RDWR | O_CREAT);
I'm new to C programming so I'm limited to what I can find with a google search and nothing there has helped. Instead, of O_CREAT I did try just using write.
#include "types.h"
#include "stat.h"
#include "user.h"
int open;
int fd;
int main (void)
{
printf(1,"My Name\n");
fd = open("/home/kyle/Desktop/Projects/Assignment 2/xv6/Tom.txt", O_RDWR | O_CREAT);
write(fd, "1 2 3 4", 7);
close(fd);
}
I need it to compile, print my name, create the file, write to the file, then save and close the file
Try including
#include <fcntl.h>
#include <stdio.h>
For printing name you could try:
printf("%s\n", "My Name");

O_WRONLY undeclared (first use in this function)

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
char data [ 6 ];
main ( ) {
int len;
desc = open ( "Resultat", O_WRONLY | O_CREAT | O_EXCL, 0666 );
if ( desc != -1 ) {
len = write ( desc, &data, sizeof ( data ) );
if ( len != sizeof ( data ) )
printf ( "ERROR" );
} }
this is my code and i'm getting the error
O_WRONLY undeclared (first use in this function)
O_CREAT undeclared (first use in this function)
O_EXCL undeclared (first use in this function)
How do I fix that?
#Kevin is right. On my Arch installation, according to man fcntl.h, you need to #include <fcntl.h> to get access to O_WRONLY.
To use open(), you also need to #include <sys/stat.h>.
I have tried this code in my machine (Ubuntu 12.0.4). But I didn't get any error messages like you got.
According to the man page of open() you are probably missing #include <sys/stat.h>.
Manual pages for open(2):
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
Please verify that you have each and every one of the required includes.

Using MinGW how can I create a file without the read-only file attribute?

When I attempt to overwrite an existing file, I get a "permission denied" error.
I noticed that the file which is created has the "Read-only" attribute set. When I manually unset this I can then overwrite the file. Is there some flag I can pass to open() which will automatically unset this when I create a file?
Below is a bare bones example which illustrates the problem. The first run works, but the second produces the "permission denied" error.
Thanks,
Zach (New to MingW/Windows 7)
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
int main(int argc, char ** argv) {
int fid;
double data = 12.0;
if ( (fid = open("junk.data", O_WRONLY | O_CREAT | O_BINARY)) == -1 ) {
printf("ERROR opening.\n\terror is:%s\n", strerror(errno));
return 1;
}
write(fid, &data, sizeof(double));
close(fid);
return 0;
}
I tried both 0644 and S_IRUSR | S_IWUSR (with sys/stat.h included) and either works.
Make sure that you actually add it as third argument of open, instead as new term into the surrounding parentheses (as happened for me first, and compiles just fine)
open has a three-parameter variant:
int open(const char *pathname, int flags, mode_t mode);
That third parameter allows you to specify the mode bits on Unix-type systems, but should be enough to set minimal r/w permissions on windows. (Check out the man page for details.)

C: stdin and std* errs

I want to manipulate Stdin and then Std*. But I am getting the following errors,
$ gcc testFd.c
testFd.c:9: error: initializer element is not constant
testFd.c:9: warning: data definition has no type or storage class
testFd.c:10: error: redefinition of `fd'
testFd.c:9: error: `fd' previously defined here
testFd.c:10: error: `mode' undeclared here (not in a function)
testFd.c:10: error: initializer element is not constant
testFd.c:10: warning: data definition has no type or storage class
testFd.c:12: error: syntax error before string constant
The program is shown below.
#include <stdio.h>
#include <sys/ioctl.h>
int STDIN_FILENO = 1;
// I want to access typed
// Shell commands, dunno about the value:
unsigned long F_DUPFD;
fd = fcntl(STDIN_FILENO, F_DUPFD, 0);
fd = open("/dev/fd/0", mode);
printf("STDIN = %s", fd);
Updated Errors: just trying to get an example program about file descriptors to work in C, pretty lost with the err report
#include <stdio.h>
#include <sys/ioctl.h>
int main (void) {
int STDIN_FILENO;
// I want to access typed
// Shell commands, dunno about the value:
unsigned long F_DUPFD;
int fd;
const char mode = 'r';
fd = fcntl(STDIN_FILENO, F_DUPFD, 0);
/* also, did you mean `fopen'? */
fd = fopen("/dev/fd/0", mode);
printf("STDIN = %s", fd);
return 0;
}
The program execution is shown below.
$ gcc testFd.c
testFd.c: In function `main':
testFd.c:14: warning: passing arg 2 of `fopen' makes pointer from integer without a cast
testFd.c:14: warning: assignment makes integer from pointer without a cast
Try using a main method:
#include <stdio.h>
#include <sys/ioctl.h>
int main (void) {
int STDIN_FILENO = 1;
// I want to access typed
// Shell commands, dunno about the value:
unsigned long F_DUPFD;
/* also, declare the type of your variable "fd" */
int fd;
fd = fcntl(STDIN_FILENO, F_DUPFD, 0);
/* also, did you mean `fopen'? */
fd = open("/dev/fd/0", mode);
printf("STDIN = %s", fd);
return 0;
}
You forgot your main() function!!
Where's your definition of main()?
Quite apart from the fact that you don't have a main() function, your entire approach is wrong. STDIN_FILENO is a constant; assigning to it doesn't make any sense.
Try explaining what you actually want to do, with some detail, and we will be able to suggest how to go about it.

Resources