C + + loops create multilevel catalogs

Source: Internet
Author: User
Tags create directory

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

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.