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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
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: ubuntu
Posts: 2,530
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: ubuntu
Posts: 2,530
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:11 AM.
|
|
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
|
|