[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Use of creat( ) function.

Posted on 2004-04-04
6
Medium Priority
?
269 Views
Last Modified: 2010-04-22
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.
   }
}
0
Comment
Question by:achille67
4 Comments
 
LVL 8

Accepted Solution

by:
manish_regmi earned 100 total points
ID: 10754708
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
 
LVL 1

Assisted Solution

by:ravenscr98
ravenscr98 earned 100 total points
ID: 10754769
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
 
LVL 8

Expert Comment

by:da99rmd
ID: 10755583
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
 
LVL 2

Expert Comment

by:timbauer
ID: 10808902
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question