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
Solved

does the file exit?

Posted on 2004-04-04
3
250 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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 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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 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