C + +: Determine if a folder exists (exist)

Source: Internet
Author: User
Tags bool

Write the program, you need to write data in the folder, if the folder does not exist, you can not write, at the program entrance needs to be judged;

Two solutions to Windows because they belong to the system layer.

Reference: Http://stackoverflow.com/questions/8233842/how-to-check-if-directory-exist-using-c-and-winapi
1. Getfileattributesa () function

DWORD d = getfileattributesa (const char* filename); #include <windows.h>
Windows system functions to determine whether a folder exists;

Code:

#include <iostream> #include <string> #include <windows.h> using namespace std;  
    BOOL Direxists (const std::string& dirname_in) {DWORD Ftyp = Getfileattributesa (Dirname_in.c_str ());  if (Ftyp = = invalid_file_attributes) return false;  
      
    Something is wrong with your path!   if (Ftyp & File_attribute_directory) return true;  
      
    This is a directory!    return false;  
This isn't a directory!  
      
    int main (void) {std::string folder ("./test");  
    if (direxists (folder) {std::cout << "folder:" << folder << "exist!" << Std::endl;  
    else {std::cout << "folder:" << folder << "doesn ' t exist!" << Std::endl;  
      
    } std::string Nofolder ("./testno"); if (direxists (Nofolder)) {std::cout << "folder: << nofolder <<" exist!"<< Std::endl;  
    else {std::cout << "folder:" << nofolder << "doesn ' t exist!" << Std::endl;  
return 0; }

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/

2. _access () function

int access (const char *filename, int mode); #include <io.h>

Mode is set to 0, to determine whether the file exists; Returns 0, the file exists;

Code:

#include <iostream> #include <string> #include <io.h> using namespace std;  
      
    BOOL Direxists (const std::string& dirname_in) {int Ftyp = _access (Dirname_in.c_str (), 0);   if (0 = Ftyp) return true;  
    This is a directory!    else return false;  
This isn't a directory!  
      
    int main (void) {std::string folder ("./test");  
    if (direxists (folder) {std::cout << "folder:" << folder << "exist!" << Std::endl;  
    else {std::cout << "folder:" << folder << "doesn ' t exist!" << Std::endl;  
      
    } std::string Nofolder ("./testno"); if (direxists (Nofolder)) {std::cout << "folder:" << nofolder << "exist!" << std::end  
    L else {std::cout << "folder:" << nofolder << "doesn ' t exist!" << std::endl  
return 0; }

Author: csdn Blog spike_king

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.