Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Checking if a file exists in Open C and C++

From Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

The article is believed to be still valid for the original topic scope.

Article Metadata
Tested with
Devices(s): Nokia 6220 Classic
Compatibility
Platform(s): S60 3rd Edition, FP2
Article
Keywords: fstream, stat, FILE, fopen(), fclose(), fstream::open(), fstream::is_open(), fstream::close()
Created: aknyman (04 Sep 2008)
Last edited: lpvalente (23 Sep 2014)

Contents

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.


This page was last modified on 23 September 2014, at 19:43.
860 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×