Solved

Use of creat( ) function.

Posted on 2004-04-04
6
260 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
6 Comments
 
LVL 8

Accepted Solution

by:
manish_regmi earned 25 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 25 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
yum update fails 11 372
Debian 3.16.7 3 123
How to redirect a serial com to a KVM machine on Proxmox 3 250
understanding an rpm spec file 5 51
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…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now