Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Use of creat( ) function.

Posted on 2004-04-04
6
Medium Priority
?
268 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

636 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