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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
10-26-2003, 05:38 AM
|
#1
|
Member
Registered: Dec 2001
Location: Basel, Switzerland
Distribution: ubuntu
Posts: 297
Rep:
|
file exists?
Hello
I am programming c in lunux. I need to determine weather a file exists, but without opening it. Is there a way to do this?
I dont want to use open("file",0) and check the return value...
Thanks
raven
|
|
|
10-26-2003, 06:09 AM
|
#2
|
Senior Member
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113
Rep: 
|
Never mind - that was bash - dunno if it works in C.
I'm an idiot.
Last edited by slakmagik; 10-26-2003 at 06:10 AM.
|
|
|
10-26-2003, 06:40 AM
|
#3
|
Senior Member
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263
Rep:
|
look up the stat sytem call
man 2 stat
|
|
|
10-26-2003, 07:05 AM
|
#4
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep: 
|
Code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc, char **argv)
{
char *filename;
int result;
struct stat statinfo;
if (argc != 2) {
fprintf(stderr, "Usage: %s [<file>]\n", argv[0]);
return 1;
}
filename = argv[1];
result = stat(filename, &statinfo);
if (result < 0) {
if (errno == ENOENT) {
printf("File %s does not exist\n", filename);
return 0;
} else {
perror(*argv);
return 1;
}
}
printf("File %s exists, and is a ", filename);
switch (statinfo.st_mode & S_IFMT) {
case S_IFREG: printf("regular file.\n"); break;
case S_IFSOCK: printf("filesystem socket.\n"); break;
case S_IFLNK: printf("symbolic link.\n"); break;
case S_IFBLK: printf("block device file.\n"); break;
case S_IFDIR: printf("directory.\n"); break;
case S_IFCHR: printf("character device file.\n"); break;
case S_IFIFO: printf("filesystem pipe (fifo) file.\n"); break;
default: printf("unknown type of file.\n"); break;
}
return 0;
}
|
|
|
10-26-2003, 07:06 AM
|
#5
|
Member
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223
Rep:
|
Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
main()
{
struct stat BUF;
if(stat("/path/to/where/file/exists",&BUF)==-1)
{
printf("File doesn't exist\n");
}
}
Hope this does it for you
|
|
|
10-26-2003, 07:23 AM
|
#6
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep: 
|
That's not reliable.
If stat() returns -1, that doesn't necessarily mean the file does not exist.
Last edited by Hko; 10-26-2003 at 07:24 AM.
|
|
|
10-26-2003, 07:27 AM
|
#7
|
Member
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223
Rep:
|
Yeah you are right ...
You need to check errono for the correct error.
Thanks for pointing that mistake.
|
|
|
10-26-2003, 07:44 AM
|
#8
|
Member
Registered: Dec 2001
Location: Basel, Switzerland
Distribution: ubuntu
Posts: 297
Original Poster
Rep:
|
Wow :-) so many replies. Thanks for all, Now I've got it :-)
raven
|
|
|
All times are GMT -5. The time now is 08:49 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|