判断路径文件夹是否存在,不存在创建
来源:互联网 发布:女性就业歧视知乎 编辑:程序博客网 时间:2024/05/17 10:40
C/C++判断一个文件是否存在
1.
2.利用 c 语言的库的办法:
3.用FindFirstFile
1.
bool isExistFile( const char *pszFileName){
FILE *fp = fopen(pszFileName, "rb");
if(fp == NULL)
return false;
fclose(fp);
return true;
}
或}
bool isExistFile( const char *pszFileName){
fstream file;
file.open(pszFileName,ios:: in);
if(!file)
return false;
return true;
}
2.利用 c 语言的库的办法:
函数名: access
// crt_access.c
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
intmain( void )
{
if( (_access( "crt_ACCESS.C", 0 )) != -1 )
{
printf( "File crt_ACCESS.C exists\n " );
if( (_access( "crt_ACCESS.C", 2 )) == -1 )
printf( "File crt_ACCESS.C does not have write permission\n " );
}
}
#include
#include
#include
int
}
3.用FindFirstFile
#include <windows.h>
#include <string>
#include <vector>
using namespace std;
// 核查目录,若目录不存在,创建目录
boolFindOrCreateDirectory( const char* pszPath )
{
WIN32_FIND_DATA fd;
HANDLE hFind = ::FindFirstFile( pszPath, &fd );
while( hFind != INVALID_HANDLE_VALUE )
{
if ( fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
return true;
}
if ( !::CreateDirectory( pszPath, NULL ) )
{
char szDir[MAX_PATH];
sprintf_s( szDir, sizeof(szDir), "创建目录[%s]失败,请检查权限", pszPath );
::MessageBox( NULL, szDir, "创建目录失败", MB_OK |MB_ICONERROR );
return false;
}
return true;
}
//遍历目录
boolCheckDirectory( char* pszPath )
{
vector < std:: string > vtPath;
const char* sep = "\\/";
char* next_token;
char* token = strtok_s( pszPath, sep, &next_token);
while( token != NULL )
{
vtPath.push_back( token );
token = strtok_s(NULL, sep, &next_token);
}
if ( vtPath.size() > 0 )
{
if ( vtPath[ 0] == "." )
vtPath.erase( vtPath.begin() );
}
// 核查所有路径是否存在
std:: string strCurPath;
for( size_t i = 0; i < ( int)vtPath.size(); ++i )
{
strCurPath += vtPath[i];
strCurPath += '\\';
if ( !FindOrCreateDirectory( strCurPath.c_str() ) )
{
return false;
}
}
return true;
}
intmain()
{
char szPath[MAX_PATH] = "./main\\test\\hello/jump\\test\\";
CheckDirectory( szPath );
system( "pause");
return 0;
}
#include
#include
using
bool
}
//
bool
}
int
}
0 0
- 判断路径文件夹是否存在,不存在创建
- 判断文件夹是否存在,不存在创建文件夹
- 判断文件夹是否存在,不存在创建文件夹
- 判断文件夹是否存在,不存在创建文件夹
- 判断文件夹是否存在,不存在则创建
- 判断文件夹,是否存在,不存在则创建
- VC++ 判断文件夹是否存在,若不存在,创建新文件夹
- 判断文件夹是否存在,不存在则创建文件夹
- 判断文件夹是否存在,不存在则创建文件夹
- 判断某一文件夹是否存在,不存在则创建该文件夹
- 【C#】C#判断文件路径是否存在,不存在则创建文件夹
- 判断路径是否存在,不存在则创建该路径
- Java判断文件夹是否存在,不存在就创建
- php判断文件夹是否存在,不存在则创建
- 判断文件夹,是否存在,不存在则创建Qt
- Java 判断文件夹、文件是否存在,不存在则创建
- python判断文件夹是否存在,不存在即创建
- Java 判断多级路径是否存在,不存在就创建
- linux编程--gdb调试学习
- 约瑟夫环问题——legend
- Foundation Framework基础类
- javax.servlet.jsp.JspException: No bean found under attribute key XXX
- 判断网络连接状态
- 判断路径文件夹是否存在,不存在创建
- C语言中exit(0)与exit(1)有什么区别
- wince 编写服务程序
- Mina框架与Spring整合配置文件
- C/C++中判断某一文件或目录是否存在
- documentsUI下载列表点击事件流程
- 【Machine Learning】Mahout基于协同过滤(CF)的用户推荐
- 获取应用和安装手机相关信息:应用版本号,手机型号,手机品牌,Android系统版本号
- IOS中获取各种文件的目录路径的方法