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 10-26-2003, 05:38 AM   #1
raven
Member
 
Registered: Dec 2001
Location: Basel, Switzerland
Distribution: ubuntu
Posts: 297

Rep: Reputation: 31
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
 
Old 10-26-2003, 06:09 AM   #2
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
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.
 
Old 10-26-2003, 06:40 AM   #3
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
look up the stat sytem call

man 2 stat
 
Old 10-26-2003, 07:05 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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;
}
 
Old 10-26-2003, 07:06 AM   #5
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
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
 
Old 10-26-2003, 07:23 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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.
 
Old 10-26-2003, 07:27 AM   #7
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
Yeah you are right ...
You need to check errono for the correct error.

Thanks for pointing that mistake.
 
Old 10-26-2003, 07:44 AM   #8
raven
Member
 
Registered: Dec 2001
Location: Basel, Switzerland
Distribution: ubuntu
Posts: 297

Original Poster
Rep: Reputation: 31
Wow :-) so many replies. Thanks for all, Now I've got it :-)

raven
 
  


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
file not found by glob? - file exists? rgdatit Linux - Newbie 2 07-29-2005 02:16 AM
SIOCADDRT: File exists SIOCCADDRT: File Exists Failed to bring up eth0. opsraja Linux - Networking 0 01-10-2005 08:29 AM
Linker problem: can't find a file, but the file exists atlep Programming 5 08-16-2004 06:15 AM
File already exists mickstaff Programming 3 05-13-2004 03:34 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 07:14 AM.

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