How to determine whether a file or directory exists _c language in C + +

Source: Internet
Author: User
1.c++ is an easy way to do this:
Copy Code code as follows:

#include <iostream>
#include <fstream>
using namespace Std;
#define FILENAME "Stat.dat"
int main ()
{
FStream _file;
_file.open (filename,ios::in);
if (!_file)
{
cout<<filename<< "not created";
}
Else
{
cout<<filename<< "already exists";
}
return 0;
}

2. The use of the C language library approach:
Function Name: Access
Features: Determining access rights to files
Usage: int access (const char *filename, int amode);
has not used this function, today, the debugger found this function, it is very useful, especially to determine whether a file or folder exists, no need to find, the file can also detect read and write permissions, the folder can only determine whether the existence of the following excerpt from the MSDN:
int _access (const char *path, int mode);
Return Value
Each of these functions returns 0 if the file has the given mode. The function returns–1 if the named file does not exist or isn't accessible in the given mode; In the, 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 is accessed as specified b Y the value of mode. When used with directories, _access determines only whether the specified directory exists; In Windows NT, all directories have read and write access.
Mode Value Checks File for
Existence only
Write permission
Read permission
Read and Write permission
Example
Copy Code code as follows:

/* ACCESS. C:this example uses _access to check the
* File named ACCESS. C "to" 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 ");
}
}

OutputFile ACCESS. C existsfile ACCESS. C has Write permission
3. FindFirstFile (...) with API functions under Windows platform:
(1) Check whether the file exists:
Copy Code code as follows:

#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" "the" the "the" Found is%s ", findfiledata.cfilename);
FindClose (hfind);
}
return (0);
}

(2) Check whether a directory exists:
Copy Code code as follows:

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
Copy Code code as follows:

#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 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 all levels of subdirectories exist, do not exist you need to create
if (!fs::exists (Full_path))
{
Creating a multi-tiered subdirectory
BOOL BRet = fs::create_directories (Full_path);
if (false = = BRet)
{
return-1;
}
}
strFilePath = Full_path.native_directory_string ();
return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.