Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 960425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:10:53+00:00 2026-05-16T01:10:53+00:00

These two code snippets produce files with different file-permissions. Example 1 creates the expected

  • 0

These two code snippets produce files with different file-permissions. Example 1 creates the expected default file-permissions but Example 2 does not. What’s the explanation for this?

OS: Mac OS X version: 10.6.4

Xcode version: 3.2.2, 64 bit


// Example 1
FILE *fh1 = fopen("Test1.txt", "w+x");

if (fh1) {
    fwrite("TEST1", 1, 5, fh1);
    fclose(fh1);
}

Creates:
-rw-r–r– 1 me staff 5 29 Jul 00:41 Test1.txt

// Example 2
int fh2 = open("Test2.txt", O_EXCL | O_CREAT | O_WRONLY);

if (fh2 >= 0) {
    write(fh2, "TEST2", 5);
    close(fh2);
}

Creates:
———- 1 me staff 5 29 Jul 00:41 Test2.txt

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:10:53+00:00Added an answer on May 16, 2026 at 1:10 am

    When you use O_CREAT you need to add a third argument to open, the mode. For instance:

    int fh2 = open("Test2.txt",
                   O_EXCL | O_CREAT | O_WRONLY,
                   S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
    

    This would be equivalent to 0666. Be aware that this mode is then masked by the process’s umask, meaning the permissions you specify will usually be reduced a bit. A typical umask is 0022, which would result in a mode of 0666 & ~0222 = 0644, i.e. -rw-r--r--.

    From man open:

    The oflag argument may indicate that the file is to be created if it does not exist (by specifying the O_CREAT flag). In this case, open requires a third argument mode_t mode; the file is created with mode mode as described in chmod(2) and modified by the process’ umask value (see umask(2)).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why are these two snippets of code giving two different results? double sum =
What is the difference between these two code snippets? open (MYFILE, '>>data.txt'); open (MYFILE,
I have two JavaScript code snippets. These perform specific task when an 'Update' button
I have these two snippets of code (written in Java) which is more more
Is SendKeys.SendWait() just a wrapper and these two code snippets are identical? const int
Help me please to find out what's the difference between these two code snippets:
What are the differences between these two code snippets? Using type : import types
What is the difference between these two code snippets: object = nil; [object release]
The following two different code snippets seem equivalent to me: var myArray = Array();
There are two code snippets: someObject.getBla1().getBla2().performBlah(); And the second one: bla1=someObject.getBla1(); bla2=bla1.getBla2(); bla2.performBlah(); I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.