Today the project encountered the need to create files, found that C + + created files are not as simple as Java, so found on the Internet for relevant content. Record it and make it easy to use later.
Creating a file in C + + requires invoking the system interface, so different systems will have different implementations. _mkdir (char* a) in the <direct.h> file can be called under Windows, while Linux is under <sys/stat.h> int mkdir (const char * ) Path, mode_t mode) method.
#include "unitfiles.h" #ifdef win32#include <direct.h> #include <io.h> #elif linux#include <stdarg.h> #include <sys/stat.h> #endif #ifdef win32#define ACCESS _access#define MKDIR (a) _mkdir ((a)) #elif Linux#define ACCESS Access#define MKDIR (a) MKDIR ((a), 0755) #endifint Creatdir (char *pszdir) {int i = 0;int Iret;int Ilen = strlen (Pszdir) ;//At the end add/if (pszdir[ilen-1]! = ' \ \ && pszdir[ilen-1]! = '/') {Pszdir[ilen] = '/';p Szdir[ilen + 1] = '% ';} Create directory for (i = 0;i <= ilen;i + +) {if (pszdir[i] = = ' \ \ ' | | pszdir[i] = = '/') {pszdir[i] = ' + ';//If not present, create Iret = ACCESS ( pszdir,0); if (IRet! = 0) {IRet = MKDIR (Pszdir); if (IRet! = 0) {return-1;}} Support Linux, change all \ to/pszdir[i] = '/';} }return 0;}
Reference http://blog.csdn.net/mafuli007/article/details/7430730
C + + loops create multilevel catalogs