From: 1538447/1.c++ a very simple way: #include <iostream>
#include <fstream>
using namespace Std;
#define FILENAME "Stat.dat"
int main ()
{
FStream _file;
_file.open (filename,ios::in);
if (!_file)
{
cout<<filename<< "was not created";
}
Else
{
cout<<filename<< "already exists";
}
return 0;
}
2. Ways to use the C-language library:
Name of function: Access
Function: Determine access rights to a file
Usage: int access (const char *filename, int amode);
Used to never use this function, today the debugger found this function, it feels good to use, especially to determine whether a file or folder exists, no need to find, the file can also detect read and write permissions, folders can only judge whether there is, the following excerpt from MSDN:
int _access (const char *path, int mode );
Return Value
Each of these functions returns 0 if the file has the the given mode. The function returns–1 if the named file does not exist or was not accessible in the given mode; Errno is set as follows:
Eacces
Access Denied:file ' s permission setting does not allow specified access.
ENOENT
Filename or path not found.
Parameters
Path
File or directory path
Mode
Permission setting
Remarks
When used with files, The _access function determines whether the specified file exists and can be accessed a s specified by the value of mode . When used and directories, _access determines only whether the specified directory exists; In Windows NT, all directories has read and write access.
Mode Value Checks File for
XX existence only
Write permission
Read permission
Read and Write permission
Example
/* ACCESS. C:this example uses _access to check the
* file named "ACCESS. C "to-see if it exists and if
* Writing is allowed.
*/
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
void Main (void)
{
/* Check for existence */
if (_access ("Access. C ", 0))! =-1)
{
printf ("File ACCESS. C exists ");
/* Check for Write permission */
if (_access ("Access. C ", 2))! =-1)
printf ("File ACCESS. C has Write permission ");
}
}
Output
File ACCESS.C existsFile ACCESS.C has write permission
3. Under Windows platform, use the API function FindFirstFile (...):
(1) Check whether the file exists:
#define _WIN32_WINNT 0x0400
#include "Windows.h"
Int
Main (int argc, char *argv[])
{
Win32_find_data Findfiledata;
HANDLE Hfind;
printf ("Target file is%s.", argv[1]);
Hfind = FindFirstFile (argv[1], &findfiledata);
if (hfind = = Invalid_handle_value) {
printf ("Invalid File Handle. Get last Error reports%d ", GetLastError ());
} else {
printf ("The first file found is%s", findfiledata.cfilename);
FindClose (hfind);
}
return (0);
}
(2) Check whether a directory exists:
Check whether the directory exists:
BOOL Checkfolderexist (const string &strpath)
{
Win32_find_data WFD;
BOOL RValue = false;
HANDLE hfind = FindFirstFile (Strpath.c_str (), &WFD);
if ((hfind! = invalid_handle_value) && (Wfd.dwfileattributes & File_attribute_directory))
{
RValue = true;
}
FindClose (hfind);
return rValue;
}
4. The exists function of the FileSystem class library using boost
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/convenience.hpp>
int GetFilePath (std::string &strfilepath)
{
String strpath;
int nres = 0;
Specify the path
strpath = "D:/mytest/test1/test2";
namespace fs = Boost::filesystem;
Portability of paths
FS::p Ath Full_path (Fs::initial_path ());
Full_path = Fs::system_complete (fs::p Ath (strpath, fs::native));
Determine if the subdirectories exist, do not exist, and need to create
if (!fs::exists (Full_path))
{
Create a multi-tiered subdirectory
BOOL BRet = fs::create_directories (Full_path);
if (false = = BRet)
{
return-1;
}
}
strFilePath = Full_path.native_directory_string ();
return 0;
}
Determine whether a file or directory exists in C/