title: Setting file permissions when opening a file with ofstream
body: <p>Is there a way in C++'s standard libraries (or linux sys/stat.h, sys/types.h, sys/.... libraries) to set the file permissions of a file when creating it with <code>ofstream</code> (or using something else from those libraries)?</p>
  
  <p>When I create a file it just gets created with some default set of file permissions (I assume whatever the current <code>umask</code> is), but I want to explicitly set the permissions to something other than the default (ex. <code>600</code>), and I can't just set the umask before starting the program (b/c others will run it). </p>
  
  <pre><code>// Example of creating a file by writing to it
  ofstream fp(filename.c_str())
  
  /* write something to it */
  </code></pre>
  
  <p>Is there a way to do this in C++ or if not, a way to set the <code>umask</code> within the C++ program?</p>
  
  <p>For example, in C's standard library you <a href="https://stackoverflow.com/a/2245212/2487336">can just do</a>:</p>
  
  <pre><code>open(filename, O_RDWR|O_CREAT, 0666)
  </code></pre>
  
  <p>but I don't want to resort to using the C function, as it'd be nice to be able to use the functions associated with <code>fstream</code> objects.</p>
  
  <p>(<em>Side Note: there was a <a href="https://stackoverflow.com/questions/6274598/how-do-i-set-file-permissions-when-opening-a-file-in-c">question</a> whose title was exactly what I was looking for, but it turned out to be unrelated.</em>)</p>