Results 1 to 8 of 8

Thread: create a file with open.

  1. #1
    Join Date
    Nov 2008
    Location
    Maine
    Beans
    1,126
    Distro
    Ubuntu 10.04 Lucid Lynx

    Question create a file with open.

    Hi everyone!
    I want to write a simple program in C that creates a file with 775 permissions, using the open command. Im not sure which mode arguments I need to make that happen with open.
    Can someone help me out?
    heres what I have:


    Code:
    #include <sys/types.h>   //Specified in man 2 open
    #include <stdlib.h>
    #include <errno.h>  	 //Allows use of error numbers
    #include <fcntl.h>       //Specified in man 2 open
    #include <stdio.h>
    
    int main(int argc, char ** argv){ 
      FILE *fp;
      fp = open("argv[1]", "O_CREAT", "MODE???");
    
      close(fp);  /* Close the file */
    return 0;
    }
    ~Conradin~

  2. #2
    Join Date
    Oct 2011
    Beans
    33

    Re: create a file with open.

    You should run this command:
    Code:
    man 2 open
    One thing you might notice is that open() returns an int value, which is meant to represent a file descriptor. If you want to work with a FILE pointer, then you should use fopen(), fclose(), etc.

    Typically open() is used for low-level I/O. For normal stuff, like reading or creating a file, fopen() is used.


    P.S. If you choose to use fopen() to create the file, you can subsequently use chmod() to set its permission mode. See "man 2 chmod" for details on this particular function.
    Last edited by gateway67; February 12th, 2012 at 11:42 PM.

  3. #3
    Join Date
    Nov 2008
    Location
    Maine
    Beans
    1,126
    Distro
    Ubuntu 10.04 Lucid Lynx

    Question Re: create a file with open.

    yeah, Im on man open(2), Im confused about what to do with multiple modes for open.
    Code:
    #include <sys/types.h>   //Specified in man 2 open
    #include <stdlib.h>
    #include <errno.h>  	 //Allows use of error numbers
    #include <fcntl.h>       //Specified in man 2 open
    #include <stdio.h>
     
    int main(int argc, char *argv[]){
    	int fd;
    	fd = open(argv[1], O_CREAT, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
     
        return 0;
    }
    gives me errors. open is what I want, eventually, I will develop device drivers. That really doesn't matter though.
    heres my errors, this thing is acting like its never seen the open modes before...
    Code:
    gcc -Wall -o "createFile775" "createFile775.c" (in directory: /home/h/Desktop/cpro)
    createFile775.c: In function ‘main’:
    createFile775.c:12: error: ‘S_IRWXU’ undeclared (first use in this function)
    createFile775.c:12: error: (Each undeclared identifier is reported only once
    createFile775.c:12: error: for each function it appears in.)
    createFile775.c:12: error: ‘S_IRWXG’ undeclared (first use in this function)
    createFile775.c:12: error: ‘S_IROTH’ undeclared (first use in this function)
    createFile775.c:12: error: ‘S_IXOTH’ undeclared (first use in this function)
    Compilation failed.
    ~Conradin~

  4. #4
    Join Date
    Oct 2011
    Beans
    33

    Re: create a file with open.

    You need to include <sys/stat.h> to obtain those macro definitions (re-read the man page for open() a little more carefully).

  5. #5
    Join Date
    Nov 2008
    Location
    Maine
    Beans
    1,126
    Distro
    Ubuntu 10.04 Lucid Lynx

    Question Re: create a file with open.

    Code:
    $ ll balls3
    ----r-S--- 1 h h 0 2012-02-13 06:29 balls3
    $ ./createFile775  balls4
    $ ll balls4
    --wxr-S--- 1 h h 0 2012-02-13 06:30 balls4*
    When I omit the mode flags, I get a file with the permissions of balls3.
    When I put anything in quotes for the mode I get permissions consistent with balls4
    ~Conradin~

  6. #6
    Join Date
    Oct 2011
    Beans
    33

    Re: create a file with open.

    Check the 'umask' setting in your working environment.
    Code:
    umask
    In my working environment it is set to 0002.

    Btw, from the code you posted earlier, I got the following to work:
    Code:
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    int main(int argc, char *argv[])
    {
        int fd = open(argv[1], O_CREAT, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
    
        close(fd);
    
        return 0;
    }
    The file that is created has permissions 775.

  7. #7
    Join Date
    Nov 2008
    Location
    Maine
    Beans
    1,126
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: create a file with open.

    How do i set my umasq? is that in the bashrc?
    ~Conradin~

  8. #8
    Join Date
    Nov 2008
    Location
    Maine
    Beans
    1,126
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: create a file with open.

    Thank you everyone! I got it!
    Code:
    #include <sys/types.h>   //Specified in man 2 open
    #include <sys/stat.h>
    #include <stdlib.h>
    #include <errno.h>  	 //Allows use of error numbers
    #include <fcntl.h>       //Specified in man 2 open
    #include <stdio.h>
     
    int main(int argc, char *argv[]){
    	int fd;
    	fd = open(argv[1], O_CREAT, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
    			
         return 0;
    }
    ~Conradin~

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •