Jump to content

  • Log In with Google      Sign In   
  • Create Account

How to check if a file exists in C?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 Maquiavel   Members   -  Reputation: 686

Like
0Likes
Like

Posted 27 August 2004 - 06:35 AM

Hello, In PHP we have the function "file_exists" that returns true in success, e.g.:
<?php
if(file_exists("files/model.md2")) {
    printf("File found!");
}
?>

And what about C? Which function has a feature like this? Thanks.

Sponsor:

#2 Alan Kemp   Members   -  Reputation: 772

Like
0Likes
Like

Posted 27 August 2004 - 06:42 AM

Just try opening it for reading. If it suceeds the file exists.


bool file_exists(const char * filename)
{
if (FILE * file = fopen(filename, "r"))
{
fclose(file);
return true;
}
return false;
}





Alan

/edit: Ahem. Nothing to see here, move along. Thanks Oxyd

[Edited by - AlanKemp on August 27, 2004 1:42:36 PM]

#3 KulSeran   Members   -  Reputation: 2538

Like
0Likes
Like

Posted 27 August 2004 - 06:44 AM

[code=auto:0]
FILE *istream;
if ( (istream = fopen ( "file.txt", "r" ) ) == NULL )
{
printf ( "file non-existant!\n" );
}
else
{
printf ( "file exists!\n" );
fclose ( istream );
}
[\code]

#4 Oxyd   Members   -  Reputation: 596

Like
0Likes
Like

Posted 27 August 2004 - 06:46 AM


bool file_exists(const char * filename)
{
if (FILE * file = fopen(filename, "r")) //I'm sure, you meant for READING =)
{
fclose(file);
return true;
}
return false;
}



#5 Maquiavel   Members   -  Reputation: 686

Like
0Likes
Like

Posted 27 August 2004 - 06:47 AM

Thanks guys for the replies!

#6 dede   Members   -  Reputation: 132

Like
0Likes
Like

Posted 27 August 2004 - 06:48 AM

For the C++ in you...

std::fstream foo;

foo.open("bar");

if(foo.is_open() == true)
std::cout << "Exist";
else
std::cout << "Doesn't Exist";




#7 Maquiavel   Members   -  Reputation: 686

Like
0Likes
Like

Posted 27 August 2004 - 06:57 AM

Oh, I was so lazy that I couldn't access google [flaming]
And I googled now and found this function:´


#include<sys/stat.h>

int file_exists (char * fileName)
{
struct stat buf;
int i = stat ( fileName, &buf );
/* File found */
if ( i == 0 )
{
return 1;
}
return 0;

}



[google]

Thanks again for your replies.

#8 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 22 September 2004 - 12:08 PM

"open" and "stat" are a bit of an overkill.
"access" is a much lighter function..

#9 dalleboy   Members   -  Reputation: 324

Like
0Likes
Like

Posted 23 September 2004 - 07:22 AM

Quote:
Original post by Anonymous Poster
"open" and "stat" are a bit of an overkill.
"access" is a much lighter function..

Add one more vote on 'access'.

Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS