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