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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:29:13+00:00 2026-06-15T02:29:13+00:00

I recently started learning unix and I am trying out some simple programs related

  • 0

I recently started learning unix and I am trying out some simple programs related to files.
I am attempting to change the access permission of a file through code using the function F_SETFL.
I created the file with only write permission and now I am trying to update the permissions through the code.
But all the permissions are getting reset.

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

int main(void) {

int fd =0;
int fileAttrib=0;

/*Create a new file*/

fd = creat("/u01/TempClass/apue/file/tempfile.txt",S_IWUSR);

fileAttrib = fcntl(fd,F_GETFL,0);
if(fileAttrib < 0 ) {
    perror("An error has occurred");
}

printf("file Attribute is %d \n",fileAttrib);

switch(fileAttrib) {

case O_RDONLY:
    printf("Read only attribute\n");
    break;
case O_WRONLY:
    printf("Write only attribute\n");
    break;
case O_RDWR:
    printf("Read Write Attribute\n");
    break;
default:
    printf("Somethng has gone wrong\n");
}

int accmode = 0;

//accmode = fileAttrib & O_ACCMODE;
accmode = 777;

fileAttrib = fcntl(fd,F_SETFL,accmode);
if(fileAttrib < 0 ) {
    perror("An error has occurred while setting the flags");
}

printf("file Attribute is %d \n",fileAttrib);

/*Print the new access permissions*/

switch(fileAttrib) {

case O_RDONLY:
    printf("New Read only attribute\n");
    break;
case O_WRONLY:
    printf("New Write only attribute\n");
    break;
case O_RDWR:
    printf("New Read Write Attribute\n");
    break;
default:
    printf("New Somethng has gone wrong\n");
}

exit(0);
}

And this is my output

file Attribute is 1

Write only attribute

file Attribute is 0

New Read only attribute

Could someone tell me the right way to set the updated flags.? I referred the documentation but still not quite clear.

  • 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-06-15T02:29:14+00:00Added an answer on June 15, 2026 at 2:29 am

    You should use chmod or fchmod to change files’ permitions. chmod with argu file’s path and fchmod with fd.

    fcntl ‘s flag F_SETFL can only set O_APPEND, O_ASYNC, O_DIRECT, O_NOATIME, and O_NONBLOCK flags.

    F_SETFL (long)
    Set the file status flags to the value specified by arg. File access mode (O_RDONLY, O_WRONLY, O_RDWR) and
    file creation flags (i.e., O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC) in arg are ignored. On Linux this command can
    only change the O_APPEND, O_ASYNC, O_DIRECT, O_NOATIME, and O_NONBLOCK flags.

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

Sidebar

Related Questions

I've recently started learning/using django; I'm trying to figure out a way to have
I recently started learning Emacs . I went through the tutorial, read some introductory
I've just recently started learning HTML/CSS and I've been trying to teach myself sound
I have recently started learning wpf and am trying to use mvvm. My understanding
I recently started learning JMS and so far understood some of the concepts. However
I recently started learning Haskell and I'm trying to rewrite something I did for
I have recently started learning spring security and trying to incorporate it into my
I recently started learning about IPC and I have some issues. I wrote a
I recently started learning Scala and started a little project to create a simple
I recently started learning Objective-C, and I am working on an iOS app as

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.