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

x
?
Solved

does the file exit?

Posted on 2004-04-04
3
Medium Priority
?
267 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
[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
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 150 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

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