You Are Here:

Community: Wiki

This page was last modified on 14 October 2008, at 12:17.

CS001101 - Checking if a file exists in C and C++

From Forum Nokia Wiki



ID CS001101 Creation date September 16, 2008
Platform S60 3rd Edition, FP2 Tested on devices Nokia 6220 Classic
Category Open C/C++ Subcategory Files/Data


Keywords (APIs, classes, methods, functions): fstream, stat, FILE, fopen(), fclose(), fstream::open(), fstream::is_open(), fstream::close()

Overview

This code snippet shows how to check if a certain file exists using C or C++. The portable way that works in C and C++ is to check if a file exists with the stat() function. This function returns the file attributes of a file and does not attempt to open the file. The other, alternative way is to simply try to open the file and see if this succeeds or fails.

Note: In order to use this code, you need to install the Open C/C++ plug-in.

This snippet can be self-signed.

MMP file

The following libraries are required:

LIBRARY  libstdcpp.lib
LIBRARY libc.lib
LIBRARY euser.lib

Source file

#include <stdio.h>    //FILE, fopen(), fclose()
#include <sys/stat.h> //stat, stat()
#include <string> //string
#include <fstream> //fstream
#include <iostream> //cout
 
using namespace std;
 
bool OpenCFileExists(const char* filename)
{
FILE* fp = NULL;
 
//this will fail if more capabilities to read the
//contents of the file is required (e.g. \private\...)
fp = fopen(filename, "r");
 
if(fp != NULL)
{
fclose(fp);
return true;
}
fclose(fp);
 
return false;
}
 
bool OpenCppFileExists(const string& filename)
{
fstream fin;
 
//this will fail if more capabilities to read the
//contents of the file is required (e.g. \private\...)
fin.open(filename.c_str(), ios::in);
 
if(fin.is_open())
{
fin.close();
return true;
}
fin.close();
 
return false;
}
 
bool FileExists(const char* filename)
{
struct stat info;
int ret = -1;
 
//get the file attributes
ret = stat(filename, &info);
if(ret == 0)
{
//stat() is able to get the file attributes,
//so the file obviously exists
return true;
}
else
{
//stat() is not able to get the file attributes,
//so the file obviously does not exist or
//more capabilities is required
return false;
}
}
bool FileExists(const string& filename)
{
return FileExists(filename.c_str());
}
 
//the simple test program for above functions
int main()
{
string filename;
 
cout << "Enter the filename (empty line quit):" << endl;
cout << ">";
 
while(getline(cin, filename))
{
string result;
 
if(filename.size() == 0)
break;
 
OpenCFileExists(filename.c_str()) ?
result = " found!" : result = " not found!";
cout << "OpenCFileExists: " << filename << result << endl;
 
OpenCppFileExists(filename) ? result = " found!" : result = " not found!";
cout << "OpenCppFileExists: " << filename << result << endl;
 
FileExists(filename) ? result = " found!" : result = " not found!";
cout << "FileExists: " << filename << result << endl;
 
cout << ">";
}
 
 
return 0;
 
}


Postconditions

The test program prompts the user to enter file names and uses example functions to test if these files exist until an empty line is entered and the program ends.

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia