Use of creat( ) function.

struct stat stat_p;
if (stat (filename, &stat_p) == -1)
{
   if (errno == ENOENT)
   {
      // file does not exist (or file name is emtpy)
  //i would like to use creat( ) to creat a new one but i am not sure how to use it.
   }
}
achille67Asked:
Who is Participating?
 
manish_regmiConnect With a Mentor Commented:
hi,
 I think the better way is my previous post.

Creat is not that difficult.
int creat(const char *pathname, mode_t mode);

just do man creat
or
http://www.die.net/doc/linux/man/man2/creat.2.html

regards manish
0
 
ravenscr98Connect With a Mentor Commented:
achille67:

Have you tried using the "creat" function is a simple program by itself to see how it works? Once you know what it is doing, it is easy to add it to that code snippet you got as a solution to "does the file exit?"

"creat" has two arguments.  The first is the file name path and the second is the desired file permissions.  The man page for "creat" lists the options.  It returns an integer file descriptor or -1 for error.

Give it a try by itself and if it doesn't work, post your code for comment.

I've been following the questions that you have been asking.  There are a couple of books that I think you would find helpful.

Brian W. Kernighan and Dennis M. Ritchie, "The C Programming Language," Second Edition.  This is THE BOOK for programming C and will answer your questions about file io and using loops to read/write files.  It is written for serious programmers.  If you need a more gentle introduction, look for a book such as "A Book On C" by Kelley and Pohl.

W. Richard Stevens, "Advanced Programming in the UNIX Environment."  This book covers the wonderful world of UNIX programming.  The first few chapters cover the file system, including input/output, creating files, and retrieving file information.
0
 
da99rmdCommented:
Se the man page for open:
man open
here you have all the settings for open you can sett it so it creates the file if its not exists.

/Rob
0
 
timbauerCommented:
The trick is seeing all the different flavors flags you can add and what permissions you want to give the file.
"creat" is just a special case of "open".

Here is some code to show and example:

int fd; /*file descriptor*/
int flags;
mode_t permissions;

/* O_CREAT specifies to create the file, the O_EXCL is optional and tells the call to
 *fail if the file already exists.
 *O_RDWR means we want  read write access to the file.
 *O_WRONLY and O_RDONLY are also different valid options */
flags =  O_CREAT|O_EXCL|O_RDWR;

/* We want the file readable and writeable for the current user
 *These are  flags that chmod sets.*/
permissions =  S_IRUSR|S_IWUSR;
fd = open( filename, flags, permissions  );

If you check the man page, make sure you look in section 2 since you want info on the "system call" open.
(Note: "creat" just turns out to be a special case of open)

man 2 open


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.