Skip to content

Instantly share code, notes, and snippets.

@SLonger
Last active February 9, 2017 01:52
Show Gist options
  • Save SLonger/db30a94f9e9ad8b48ea20f705d037355 to your computer and use it in GitHub Desktop.
Save SLonger/db30a94f9e9ad8b48ea20f705d037355 to your computer and use it in GitHub Desktop.
create file contain directory , create parent directories as needed
#include <io.h>
#include <direct.h>
#define MAX_PATH 256
void CreateDir(const char* pPath)
{
const char *lastpoint;
const char* pCur = pPath;
char tmpPath[MAX_PATH];
int pos=0;
memset(tmpPath,0,sizeof(tmpPath));
if(-1 != access(pPath,0))
return;
if((lastpoint=strrchr(pPath,'/'))==NULL){
FILE * fp=fopen(pPath,"a+");
fclose(fp);
return ;
}
while(pCur++!=lastpoint)
{
tmpPath[pos++] = *(pCur-1);
if(*pCur=='/' || pCur==lastpoint)
{
if(0!=access(tmpPath,0)&&strlen(tmpPath)>0)
{
mkdir(tmpPath);
}
}
}
FILE * fp=fopen(pPath,"a+");
fclose(fp);
return ;
}
int main()
{
const char* str="aa/bb/aa.txt";
CreateDir(str);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment