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 7415037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:13:08+00:00 2026-05-29T07:13:08+00:00

I generally don’t like asking what’s wrong with my code questions, but this is

  • 0

I generally don’t like asking “what’s wrong with my code” questions, but this is my last hope.

I’m doing a project in which I have to write to files, and I’m trying to do it using system calls (Linux). So I’m using unistd.h, which provides the functions: int open( char* filename, int flags, int mode ) and int write( int fd, void* ptr, int numbytes ). I use write all the time with no problem, but that’s normally with the standard out and standard error file descriptors.

So I use this code to open the file:

int flags = O_WRONLY;
if( !exists( "testfile2.txt" ) ) {
    flags |= O_CREAT;
}
int mode = S_IROTH | S_IWOTH | S_IXOTH;
int filedesc = open( "testfile2.txt", flags, mode );

And then this line to write to the file:

int written = write(filedesc, "abcdefghijklmnopqrstuvwxyz",
    strlen("abcdefghijklmnopqrstuvwxyz" ) );

And finally, I close the file with this:

int closed = close( filedesc );

The problem is that when I try to write to the file when it didn’t already exist, I get a message saying “permission denied”. When I open it in vi and ignore the permissions, the file appears to be empty. However, if the file existed initially, it writes to it and I can read it just fine. Does anyone have any clue what’s going wrong or if I’m missing something?

Thanks in advance.

  • 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-29T07:13:09+00:00Added an answer on May 29, 2026 at 7:13 am

    According to the docs, O_CREAT uses the permission bits only if the file does not exists. You pass a permission mask of 007, denying “owner” and “group” all rights on the file. Use at least S_IRUSR | S_IWUSR in the mode flags:

    x@y:~/tmp$ touch test
    x@y:~/tmp$ cat test
    x@y:~/tmp$ chmod 007 test
    x@y:~/tmp$ cat test
    cat: test: Permission denied
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Generally don't do OO-programming in Python. This project requires it and am running into
I don't know where I went wrong but I'm generally trying to write a
In general, I don't like to keep code (BaseClasses or DataAccess Code) in the
I don't expect a straightforward silver bullet answer to this, but what are the
I generally don't use Interface Builder (I hate it). But I am being forced
As a user, I generally don't like working with calendar GUI (e.g. on airline
I'm generally of the opinion that if it ain't broke don't fix it, but
I don't need it to be too secure. Even md5, which is generally broken,
I don't even know where to begin to look for something like this, or
I generally like the use of the pager in git, but for git stash

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.