0

I am trying to create a file, but it is opening in locked mode. How to make it in read write execute mode?

switch(choice){
case 1: printf("\n Enter the file: ");
    scanf("%s", file);
    open(file, O_CREAT, S_IRWXG);
    break;
mico
  • 12,730
  • 12
  • 59
  • 99
user2899997
  • 3
  • 1
  • 4
  • Because you omit O_RDONLY, O_WRONLY and O_RDWR, you are actually opening the file in O_RDONLY mode, for all you also create it. The `S_IRWXG` flag sets `---rwx---` mode, modified by `umask`. It's most succinct to write the mode in octal: `0777` if you're sufficiently confident that you won't be attacked. Or use `S_IRWXU|S_IRWXG|S_IRWXO`. But that is still modified by `umask`. – Jonathan Leffler Oct 20 '13 at 20:56

1 Answers1

1

The third argument of open doesn't actually have to be one of the defined flags. If you want it in +rwe mode for all users, just change your code to

open(file, O_CREAT, 0777);

EDIT: If you'd prefer to use the flags. Just combine them with the | command. You'll end up actually passing the same value in, but many people prefer to use the flags.

Happington
  • 454
  • 2
  • 8