Solved

does the file exit?

Posted on 2004-04-04
3
239 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

12 Experts available now in Live!

Get 1:1 Help Now