14

This is the first time I am using open() from:

#include <fcntl.h>

I am trying to create two files:

int fd;
int fd2;
char *tmpname = "./TMPFILE";
printf( "Temporary file created\n ");
char *tmpname2 = "./TMPFILE2";
printf( "Temporary file two created\n ");
fd = open(tmpname, O_WRONLY | O_APPEND);
fd2 = open(tmpname2, O_WRONLY | O_APPEND);

I am trying to create the files in the current working directory that can be written and appended to.

This compiles and runs, but my concern is that when I check my directory to see if the files were created, they aren't listed.

My question is does open() only make temporary files that are removed after the program runs or did I screw something up?

2
  • u did do "ls -ail" - ie force the inclusion of files starting with "." Commented Feb 11, 2015 at 23:41
  • @pm100 Yeah, I tried that and nothing. Commented Feb 11, 2015 at 23:44

1 Answer 1

29

When creating a file, you need a third parameter to open (the mode). If you don't do this, unpredictable things happen.

Also, if you want to create a file if it's not there, you will need O_CREAT or'd in, i.e.

fd = open(tmpname, O_WRONLY | O_APPEND | O_CREAT, 0644);

O_CREAT (roughly speaking) creates the file if it isn't present.

From the man page:

NAME
       open, creat - open and possibly create a file or device

SYNOPSIS
       #include 
       #include 
       #include 

       int open(const char *pathname, int flags);
       int open(const char *pathname, int flags, mode_t mode);

       int creat(const char *pathname, mode_t mode);

...
       O_CREAT
              If the file does not exist it will be created.  The owner (user ID) of
              the  file  is  set to the effective user ID of the process.  The group
              ownership (group ID) is set either to the effective group  ID  of  the
              process  or  to  the  group  ID  of the parent directory (depending on
              filesystem type and mount options, and the mode of the  parent  direc‐
              tory,  see  the  mount  options  bsdgroups and sysvgroups described in
              mount(8)).

              mode specifies the permissions to use in case a new file  is  created.
              This  argument must be supplied when O_CREAT is specified in flags; if
              O_CREAT is not specified, then mode is ignored.  The effective permis‐
              sions  are  modified by the process's umask in the usual way: The per‐
              missions of the created file are (mode & ~umask).  Note that this mode
              applies  only to future accesses of the newly created file; the open()
              call that creates a read-only file may well return a  read/write  file
              descriptor.

Sign up to request clarification or add additional context in comments.

6 Comments

@abligh Well, what do you know, I am an idiot haha. Thank you for your help. Next time I will just take the advice and read the manual.
Where did 0644 come from? I could not find this mode in the man pages
@PyWalker2797 it is an octal expression (leading 0) of the unix file mode. See e.g. the chmod manpage. 0644 means the owner can read+write (4+2=6), the group can read (4), and others can read (4).
@PyWalker2797 you can also use permissions-calculator.org to calculate the proper code for your permission level
Is there any reason to have both the O_WRONLY flag and O_APPEND?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.