O_WRONLY undeclared (first use in this function) - c

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

Related

C Linux shared memory error - ftruncate

I'm trying to open a shared mem file and write into it. The problem is that ftruncate is returning -1 .
Here is my code:
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include<sys/mman.h>
#include<sys/stat.h>
#include <fcntl.h>
int main(void) {
int fd;
fd=shm_open("/shmem-m", O_CREAT,0777);
printf("%d\n",fd);
int a=ftruncate(fd, 1024);
printf("%d\n",a);
void* addr=mmap(NULL, 1024, PROT_READ|PROT_WRITE, MAP_SHARED,fd, 0);
char* msg= "hola mundo!";
memcpy(addr,msg,strlen(msg));
exit(EXIT_SUCCESS);
//return 0;
}
The output is:
3
-1
Segmentation fault
Any ideas? Thank you very much
The problem is that POSIX requires the file to be opened in write mode for a call to ftruncate to succeed as stated in the ftruncate man page.
So the call to shm_open becomes shm_open("/shmem-m", O_CREAT | O_RDWR, 0777), with the O_RDWR flag set (shm_open man page).

Migrating a C program from Linux to Windows

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.

Implicit declaration of mkdir

For my question let's suppose I have two functions, both of them with the prototypes on a .h file in a library folder, and the implementation in a .c auxiliary file (shown below), and I will use both of them in my program.
calsis.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include "include/calsis.h" /* Extern header */
char folder_name[30] = "Information";
void no_args() /* Function 1 */
{
printf("Hello, world!\n");
if ( mkdir(folder_name, S_IRWXU) == -1 )
perror("Can't create a new folder");
}
void with_args(char *foo) /* Function 2 */
{
printf("Hello, world!\n");
printf("Name: %s\n", foo);
if ( mkdir(folder_name, S_IRWXU) == -1 )
perror("Can't create a new folder");
}
For something I will do later, I need in both functions to create a folder with mkdir, but, in the generation of the object file calsis.o by the compilation of the .c file with the implemented functions, the compilation with GCC gives me a warning that the mkdir function is implicity declared.
Any idea I can remove this warning?
You haven't included the header for mkdir:
From man(2) mkdir:
SYNOPSIS
#include <sys/stat.h>
#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode);

open() function error when flags are a char string pointer instead of an int

#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
int main(int argc,char *argv[])
{
int fd;
int i=1;
for(i=1;i<argc;++i)
{
char temp;
fd=open(argv[i],"O_RDWR");
if (fd==-1)
perror("file:");
while (read(fd,&temp,1)!=EOF)
{
putchar(temp);
}
}
}
I execute ./a.out a b. a and b are files in my directory. I get an error saying File exists.
The line open(argv[i],"O_RDWR") is not opening the file.
It is returning -1 since the file exists . How then should i open the file using the open system call?
fd=open(argv[i],"O_RDWR");
^ ^
You're passing a char * instead of an integer constant. Drop the ", it should be just:
fd = open(argv[i], O_RDWR);
Interestingly but likely off-topic, open must have thought you passed O_CREAT | O_EXCL, that's why it complained about the file already existing.
So what i have wriiten is right then???But the code is going into an
infinite look printing nothin
The function read(2) doesn't return EOF at and of input but rather 0.

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

Resources