Checking for file existence in C and C++
__NOTOC__
__NOEDITSECTION__
{|style=”background:#eceff2″ width=”660px” border=”1″ cellpadding=”5″ cellspacing=”0″
|-
|”’ID”’ ||
|”’Creation date”’ || September 4, 2008
|-
|”’Platform”’ || S60 3rd Edition, FP2
|”’Tested on devices”’ || Nokia 6220 Classic
|-
|”’Category”’ || Open C/C++
|”’Subcategory”’ || Files/Data
|}
{|style=”background:#eceff2″ width=”660px” border=”1″ cellpadding=”5″ cellspacing=”0″
|-
|”’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 <tt>stat()</tt> function. This function returns the file attributes of a file and does not attempt to open the file. The other alternative way is simply try to open the file and see if this fails.
”’Note”’: In order to use this code, you need to install [http://www.forum.nokia.com/info/sw.nokia.com/id/91d89929-fb8c-4d66-bea0-227e42df9053/Open_C_SDK_Plug-In.html Open C/C++ plug-in].
This snippet can be self-signed.
==MMP file==
The following libraries are required:
<code>
LIBRARY libstdcpp.lib
LIBRARY libc.lib
LIBRARY euser.lib
</code>
<!– ==Header file==
<code cpp>
</code>
–>
==Source file==
<code cpp>
#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;
}
</code>
==Postconditions==
The test program prompts user to enter filenames and uses example functions to test existence of these files until the empty line is entered and the program ends.
<!– ==See also== –>
Thank you for reading this post. You can now Leave A Comment (0) or Leave A Trackback.
Post Info
This entry was posted on Sunday, September 7th, 2008 and is filed under Insurance.You can follow any responses to this entry through the Comments Feed. You can Leave A Comment, or A Trackback.
Previous Post: Nokia: Q3 2008 market share may drop »
Next Post: Pixelmator puts new shortcuts in 1.2.3, pitches 1.3 Tempo beta »
- Would-be Japanese space tourist wants $21M back
- NASA scales back flagship Mars mission
- Bulgarian archaeologists unearth ancient chariot
- Witnesses: Large meteor streaks across Canada sky
- Solar panels on graves give power to Spanish town
- Fuel from food? The feast is over
- Fabulous flippers: Dolphins have quite the kick
- Whales rescued from Australian beach join pod
- Siberian quake triggers false US seismic reports
- Pirates sign Indian _ not Cleveland _ pitchers

