Solved

does the file exit?

Posted on 2004-04-04
3
242 Views
Last Modified: 2010-04-21
hey ****s,
How do i check if a file exits before doing something with it ?(C language)
0
Comment
Question by:achille67
3 Comments
 
LVL 33

Expert Comment

by:hongjun
ID: 10754075
You can use stat() function to check file exist


#include <sys/stat.h>
#include <errno.h>
...

struct stat stat_p;
if (stat (filename, &stat_p) == -1)
{
   if (errno == ENOENT)
   {
      // file does not exist (or file name is emtpy)
     ... // do something
   }
}

Using stat() allows you to do some more testing on the file (e.g. find out if it's really a normal file and not a link or a directory, ...)



hongjun
0
 

Author Comment

by:achille67
ID: 10754375
i am partially agree on the answer: if the file does not exist, i would to create it using creat( ) but i am not sure how i should pass the pointer to it.
0
 
LVL 8

Accepted Solution

by:
manish_regmi earned 50 total points
ID: 10754525
hi,
you can use open with O_CREAT and O_EXCL.
This will create the file and if the file exists, it will return error.
open ("/xyz", O_CREAT | O_EXCL | O_RDWR);

regards manish

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to import linux vhd to sccvmm 1 150
shell script working with --daemon 18 93
RHEL red hat 7.3 how can I upgrade the kernel? 12 840
Set-Top-Box hardware and Open Source! 8 144
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…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
This is an introductory video for CloudBerry Managed Backup. You will learn how to sign up with the service and get started in a few minutes.

911 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

22 Experts available now in Live!

Get 1:1 Help Now