c++unixsolaris

How to neglect the umask so as to create the file with given permission


I am creating a file using open function and using O_CREAT | O_EXCEL . I have passed the mode as "0666" . But by masking finally the permission allotted to it is -rw-r--r-- and not the -rw-rw-rw- . Someone told me i can use umask (011) and then reset the original mask again . But i dont know how to pass this in c++ program. This is the small snippet of What i am doing .

   # include <iostream>
   # include <stdio.h>
   # include <conio.h>
   #include <sys/types.h>
   #include <sys/stat.h>
   #include <fcntl.h>

   using namespace std;

   int main()
   {
    int fd = open("C:\\Users\\Ritesh\\Music\\music.txt", O_CREAT | O_EXCL, 0666);   
    getch();
    return 0;   
  } 

creates file C:\Users\Ritesh\Music\music.txt with permission -rw-r--r-- . I want it to be -rw-rw-rw-


Solution

  • mode_t old_mask;
    
    old_mask = umask(011);
    open( ... );
    umask(old_mask);