LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-05-2010, 02:14 PM   #1
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Check if file exists in C


Let's say I have a path stored in a string.

I would like to know:
  • Does the file exist?
  • Do I have permissions to edit it?
  • Is it a directory?
  • Can I add files to the directory it's in?
 
Old 06-05-2010, 02:49 PM   #2
imitheos
Member
 
Registered: May 2005
Location: Greece
Posts: 441

Rep: Reputation: 141Reputation: 141
have you tried the *stat functions ?
Code:
man 2 stat
 
1 members found this post helpful.
Old 06-05-2010, 03:16 PM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Sure, but I didn't really understand how to do what I wanted using stat.
 
Old 06-05-2010, 03:49 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
Sure, but I didn't really understand how to do what I wanted using stat.
  1. How many lines of the manpage did you read ?
  2. What was the first thing you do not understand ?
 
Old 06-05-2010, 04:09 PM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Sergei Steshenko View Post
How many lines of the manpage did you read ?
I'll just try to ignore the intended meaning in that. Me and the moderators will probably be happier in the end if I do so.

Anyway, I looked over the whole thing a few times before asking.

Quote:
Originally Posted by Sergei Steshenko View Post
What was the first thing you do not understand ?
Code:
RETURN VALUE
       Upon successful completion, 0 shall be returned. Otherwise, -1 shall be  returned  and
       errno set to indicate the error.

ERRORS
       The stat() function shall fail if:

       EACCES Search permission is denied for a component of the path prefix.

       EIO    An error occurred while reading from the file system.

       ELOOP  A loop exists in symbolic links encountered during resolution of the path argu‐
              ment.

       ENAMETOOLONG
              The length of the path argument exceeds {PATH_MAX} or a pathname  component  is
              longer than {NAME_MAX}.

       ENOENT A component of path does not name an existing file or path is an empty string.

       ENOTDIR
              A component of the path prefix is not a directory.

       EOVERFLOW
              The  file  size  in  bytes or the number of blocks allocated to the file or the
              file serial number cannot be represented correctly in the structure pointed  to
              by buf.

       The stat() function may fail if:

       ELOOP  More  than  {SYMLOOP_MAX}  symbolic links were encountered during resolution of
              the path argument.

       ENAMETOOLONG
              As a result of encountering a symbolic link in resolution of the path argument,
              the length of the substituted pathname string exceeded {PATH_MAX}.

       EOVERFLOW
              A value to be stored would overflow one of the members of the stat structure.

       The following sections are informative.
OK, which one of these errors mean "the file does not exist"?
 
Old 06-05-2010, 04:52 PM   #6
joakim12
Member
 
Registered: Nov 2003
Location: Estonia
Distribution: Ubuntu 18.04
Posts: 82

Rep: Reputation: 15
ENOENT A component of path does not name an existing file or path is an empty string.

Okey, my english is not very good, but I can understand that if "the path does not name an existing file" means that the file does not exist what the string path points to.

Am I right?
 
Old 06-05-2010, 05:02 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
I'll just try to ignore the intended meaning in that. Me and the moderators will probably be happier in the end if I do so.

Anyway, I looked over the whole thing a few times before asking.



Code:
RETURN VALUE
       Upon successful completion, 0 shall be returned. Otherwise, -1 shall be  returned  and
       errno set to indicate the error.

ERRORS
       The stat() function shall fail if:

       EACCES Search permission is denied for a component of the path prefix.

       EIO    An error occurred while reading from the file system.

       ELOOP  A loop exists in symbolic links encountered during resolution of the path argu‐
              ment.

       ENAMETOOLONG
              The length of the path argument exceeds {PATH_MAX} or a pathname  component  is
              longer than {NAME_MAX}.

       ENOENT A component of path does not name an existing file or path is an empty string.

       ENOTDIR
              A component of the path prefix is not a directory.

       EOVERFLOW
              The  file  size  in  bytes or the number of blocks allocated to the file or the
              file serial number cannot be represented correctly in the structure pointed  to
              by buf.

       The stat() function may fail if:

       ELOOP  More  than  {SYMLOOP_MAX}  symbolic links were encountered during resolution of
              the path argument.

       ENAMETOOLONG
              As a result of encountering a symbolic link in resolution of the path argument,
              the length of the substituted pathname string exceeded {PATH_MAX}.

       EOVERFLOW
              A value to be stored would overflow one of the members of the stat structure.

       The following sections are informative.
OK, which one of these errors mean "the file does not exist"?
First of all, is this all you read ? Because I know for sure that had you read the whole manpage and had you followed some other references in the manpage, you would have found an easy answer to your "OK, which one of these errors mean "the file does not exist"?" question.

You did not read the manpage.

Back to your question - in my case I have the following explanation:

Code:
ENOENT A component of the path path does not exist, or the path is an empty string.
.

So, can you parse that statement - saying "parse" I mean human parsing of English ?
 
Old 06-05-2010, 05:05 PM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by joakim12 View Post
ENOENT A component of path does not name an existing file or path is an empty string.

Okey, my english is not very good, but I can understand that if "the path does not name an existing file" means that the file does not exist what the string path points to.

Am I right?
Don't worry, I've seen too many non-native English speakers who understand English much better than the native ones - probably because the non-native speakers were taught English systematically, were required to parse English sentences, were required to ask question about every sentence member, were required to tell stories they were supposed to read in their own words, etc.
 
Old 06-05-2010, 06:24 PM   #9
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
By the way, MTK358, just in case you are still having difficulty parsing the English statement, and in case you have difficulties using WEB search, here is the 5-th match from Google: http://everything2.com/title/ENOENT .

I am not a native English speaker, but I think the acronym means "Error NO ENTity". And if you ever visit the second page of Google hits - http://aplawrence.com/Unixart/errors.html .
 
Old 06-05-2010, 07:15 PM   #10
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Sergei Steshenko View Post
You did not read the manpage.

Back to your question - in my case I have the following explanation:

Code:
ENOENT A component of the path path does not exist, or the path is an empty string.
.

So, can you parse that statement - saying "parse" I mean human parsing of English ?
I DID read it.

I guess you could say I read it but did not parse it.
 
Old 06-05-2010, 07:48 PM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
I DID read it.

I guess you could say I read it but did not parse it.
I have already told: read, comprehend, analyze. If you are having difficulties with it, take an English class at a community college.

Now, at the very bottom of the manpage it is written:


Code:
SEE ALSO
       access(2)
.

So, you had to also see

Code:
man 2 access
.

And, trust me, the "see also" manpage contains very simple ways to find answers to your questions.

As I have already said, you didn't read the manual.

And since you've mentioned moderators, I think they should always close such threads of yours because you do no read the manuals and do not understand English.

Since I am not sure you will read 'man 2 access', here are the relevant lines from it:

Code:
   13 DESCRIPTION
     14        access() checks whether the calling process can access the file pathname.  If pathname is a symbolic link, it is dereferenced.
     15
     16        The  mode specifies the accessibility check(s) to be performed, and is either the value F_OK, or a mask consisting of the bitwise OR of one or more of R_OK, W_OK,
     17        and X_OK.  F_OK tests for the existence of the file.  R_OK, W_OK, and X_OK test whether the file exists and grants read, write, and execute  permissions,  respec-
     18        tively.
 
1 members found this post helpful.
Old 06-07-2010, 10:36 AM   #12
imitheos
Member
 
Registered: May 2005
Location: Greece
Posts: 441

Rep: Reputation: 141Reputation: 141
Quote:
Originally Posted by MTK358 View Post
Let's say I have a path stored in a string.

I would like to know:
  • Does the file exist?
  • Do I have permissions to edit it?
  • Is it a directory?
  • Can I add files to the directory it's in?
Quote:
Originally Posted by MTK358 View Post
Anyway, I looked over the whole thing a few times before asking.
I don't want to sound disrespectful but as the other people said, it doesn't
seem you read the manpage. It has a lot of information and even includes
a example c program showing how stat works.
Quote:
int stat(const char *path, struct stat *buf);

DESCRIPTION
stat() stats the file pointed to by path and fills in buf.

All of these system calls return a stat structure

The following POSIX macros are defined to check the file type using the
st_mode field:

S_ISDIR(m) directory?

The following flags are defined for the st_mode field:

S_IRUSR 00400 owner has read permission
S_IWUSR 00200 owner has write permission
S_IXUSR 00100 owner has execute permission
S_IRGRP 00040 group has read permission
S_IWGRP 00020 group has write permission
S_IXGRP 00010 group has execute permission
S_IROTH 00004 others have read permission
S_IWOTH 00002 others have write permission
S_IXOTH 00001 others have execute permission

RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is
set appropriately.

ERRORS
EACCES Search permission is denied for one of the directories in the
path prefix of path. (See also path_resolution(7).)

ENOENT A component of path does not exist, or path is an empty string.
I think this part of the manpage explains all your questions.
 
Old 06-07-2010, 02:04 PM   #13
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
also,
you could simply do an fopen( file, "r+")

which will fail if the file is a directory
or unreadable. (only the kernel can write to a directory)

then fclose and fopen or freopen "r"

more portable too.
 
Old 06-07-2010, 02:50 PM   #14
dougbourne
LQ Newbie
 
Registered: Jun 2008
Posts: 29

Rep: Reputation: 15
you can use access()

http://linux.die.net/man/2/access
 
Old 06-09-2010, 12:55 PM   #15
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
interesting...

from that very page!
Code:
Using access() to check if a user is authorized
 to e.g. open a file before actually 
doing so using open(2) creates a security
 hole, because the user might exploit the
 short time interval between checking and opening 
the file to manipulate it.
my man page:
Code:
The access() system call is a potential 
security hole due to race conditions and 
should never be used.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Makefile:condition to check whether a file exists ? Ashok_mittal Linux - Newbie 4 12-06-2011 07:52 PM
Check File Exists shady4u Linux - General 2 02-21-2010 03:05 AM
Bash Help: Check if file exists richinsc Programming 6 01-09-2009 12:27 PM
[SOLVED] check whether a file exists or not ?? vikas027 Programming 5 06-05-2008 04:45 AM
c++ check if file exists Genjix Programming 3 03-15-2004 12:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration