C++ 自动创建文件夹

实例

#include "stdafx.h"
#include <Windows.h>   
#include <iostream>    
#include <string> 
#include <io.h>
#include <direct.h>
using namespace std;     

  
int _tmain(int argc, _TCHAR* argv[])
{ 
	char *fileName="123\\1\\2\\3\\a.txt",*tag;
	for(tag=fileName;*tag;tag++)
	{
		if (*tag=='\\')
		{
			char buf[1000],path[1000];
			strcpy(buf,fileName);
			buf[strlen(fileName)-strlen(tag)+1]=NULL;
			strcpy(path,buf);
			if (access(path,6)==-1)
			{
				mkdir(path);  //创建成功返回0 不成功返回-1
			}
		}
	}
	return 0;
}


运行结果是在.exe目录下判断123\\1\\2\\3\\是否有这些文件没有就创建。

 

a) access函数说明

int   access(const   char   *filename,   int   amode); 
amode参数为0时表示检查文件的存在性,如果文件存在,返回0,不存在,返回-1。 
这个函数还可以检查其它文件属性: 
06     检查读写权限 
04     检查读权限 
02     检查写权限 
01     检查执行权限 
00     检查文件的存在性
而这个就算这个文件没有读权限,也可以判断这个文件存在于否
存在返回0,不存在返回-1

C函数
  函数名: access 
  功 能: 确定文件的访问权限 
  用 法: int access(const char *filename, int amode);

 

b)mkdir函数

在工作目录下,建立新的子目录

 //创建成功返回0 不成功返回-1
 

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值