?
Solved

does the file exit?

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

752 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