Is there a better way than simply trying to open the file?
int exists(const char *fname)
{
FILE *file;
if ((file = fopen(fname, \"r\")))
{
From the Visual C++ help, I'd tend to go with
/* ACCESS.C: This example uses _access to check the
* file named "ACCESS.C" to see if it exists and if
* writing is allowed.
*/
#include
#include
#include
void main( void )
{
/* Check for existence */
if( (_access( "ACCESS.C", 0 )) != -1 )
{
printf( "File ACCESS.C exists\n" );
/* Check for write permission */
if( (_access( "ACCESS.C", 2 )) != -1 )
printf( "File ACCESS.C has write permission\n" );
}
}
Also worth noting mode values of _access(const char *path,
int mode
)
:
00: Existence only
02: Write permission
04: Read permission
06: Read and write permission
As your fopen
could fail in situations where the file existed but could not be opened as requested.
Edit: Just read Mecki's post. stat()
does look like a neater way to go. Ho hum.