5

I am working on a project for a class and we were given a .c file containing the following code:

int fd = -1;
if (fd < 0)
{
  fd = open ("my_dev", O_RDWR);
  if (fd < 0)
  {
    perror ("open");
    return -1;
  }
...

So I understand that it is trying to open a file "my_dev" with read/write permissions, and then is returning the file descriptor on success or a negative value on failure, but what I dont understand is why it is giving me "permission denied" consistently. I tried to use this code:

int des = open("my_dev", O_CREAT | O_RDWR, 0777);
...
close(des)

to open/create the file (this is called before the other block), but this does not work, and yet if I just use this instead:

FILE* file = fopen("my_dev","w+");
fprintf(file,str);
fclose(file);

I can write to the file, meaning I have write permissions. Now normally, I would just use fopen and fprintf for everything, but for this project, we sort of have to use the teacher's .c file which is going to try to use

 open()

which is going to give a "permission denied" error which is in turn going to screw up my code.

I guess my question is how fopen and open relate to each other? Everyone seems to be able to recite that open is a system call whereas fopen is a standard lib function, but I cant seem to find a clear answer for how I can create a file with fopen() that can be opened by open() without a "permission denied" error, or how I can create a file with open() which I can then write to, close and open again with open().

In short, how do I create a file in C that I can write to and open later with open(O_RDWR)?

Sorry if this is a little piecey, im super tired.

PS: It should be noted that I am compiling and running on a university computer, so permissions may be "weird" BUT it should be noted that if I create the file with the terminal command "dd" open() will work, and furthermore, I clearly have SOME write permissions since I can indeed write to the file with fopen and fprintf

jotik
  • 17,044
  • 13
  • 58
  • 123
botch
  • 167
  • 1
  • 10

1 Answers1

5

fopen is a library function that provided by the standard C runtime, it returns a stream and you can call stream functions on it, like fscanf, fprintf, or fread, fwrite.

open is usually a system call on unix-like systems, provided by the operating system kernel, it returns an file descriptor, you can call IO functions with the fd, like read, write.

Generally fopen is implemented using open underline.

If you want to use standard stream functions on a file descriptor, you can use the posix api, fdopen, which takes a fd, and returns a FILE* stream.

fluter
  • 13,238
  • 8
  • 62
  • 100
  • I also tried FILE* file = fdopen(des,"w+") which cannot even write to the file, apparently. – botch Apr 25 '16 at 06:49
  • 1
    The des needs to be in O_WRONLY or O_RDWR before calling this. – fluter Apr 25 '16 at 06:50
  • AFAIK if you use `C` the `open` is a library function too. – red0ct Apr 25 '16 at 07:09
  • This all depends on the platform and the implementation, for example, glibc provides wrappers for many system calls, otherwise you would be calling something like syscall(SYS_OPEN, "file", mode) – fluter Apr 25 '16 at 07:11