Determine whether a file or directory exists in C/

Source: Internet
Author: User

A simple way to determine whether a file or directory exists 1.c++ in C + +: #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 is accessed as specified by the value of  mode . When used with 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 (), &AMP;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/

Related Article

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.