Namespaces
Variants
Actions
Revision as of 20:03, 26 October 2012 by lpvalente (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

Jump to: navigation, search
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 (26 Oct 2012)

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.


493 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved