文件
ifstream
在C++中,可以利用ifstream文件输入流,当我们直接使用ifstream来创建文件输入流的时候,如果文件不存在则流创建失败。
ifstream fin("hello.txt");
if (!fin)
{
std::cout << "can not open this file" << endl;
}
fstream
fstream _file;
_file.open(FILENAME,ios::in);
if(!_file)
{
cout<<FILENAME<<"没有被创建";
}
else
{
cout<<FILENAME<<"已经存在";
}
_taccess
Generic-Text Routine Mappings:
| TCHAR.H Routine | _UNICODE & _MBCS Not Defined | _UNICODE Defined | _UNICODE Defined |
| _taccess | _access | _access |_waccess |
返回值
mode Value Checks File For
00 Existence only
02 Write permission
04 Read permission
06 Read and write permission
Example:
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
// Check for existence.
if( (_access( "crt_ACCESS.C", 0 )) != -1 )
{
printf_s( "File crt_ACCESS.C exists.\n" );
// Check for write permission.
// Assume file is read-only.
if( (_access( "crt_ACCESS.C", 2 )) == -1 )
printf_s( "File crt_ACCESS.C does not have write permission.\n" );
}
}
https://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=ZH-CN&k=k(%22TCHAR%2f_TACCESS%22);k(_TACCESS);k(DevLang-%22C%2B%2B%22);k(TargetOS-WINDOWS)&rd=true;k(_TACCESS);k(DevLang-%22C%2B%2B%22);k(TargetOS-WINDOWS)&rd=true)
FindFirstFile
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
printf ("Target file is %s. ", argv[1]);
hFind = FindFirstFile(strPath.c_str(), &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("Invalid File Handle. Get Last Error reports %d ", GetLastError ());
}
else
{
printf ("The first file found is %s ", FindFileData.cFileName);
FindClose(hFind);
}
目录
FindFirstFile
WIN32_FIND_DATA wfd;
bool rValue = false;
HANDLE hFind = FindFirstFile(strPath.c_str(), &wfd);
if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY))
{
std::cout << "this file exists" << endl;
}
FindClose(hFind);
_Tstat
在windows中可以使用_stat() 函数。
声明:int _stat(const char path, struct _stat buffer);
这个函数使用起来非常方便,如下:
struct _stat fileStat;
if ((_stat(dir.c_str(), &fileStat) == 0) && (fileStat.st_mode & _S_IFDIR))
{
isExist = true;
}
_S_IFDIR 是一个标志位,如果是目录的话,该位就会被系统设置。
在linux底下也有相对应的函数stat();
使用方法基本相同:
struct stat fileStat;
if ((stat(dir.c_str(), &fileStat) == 0) && S_ISDIR(fileStat.st_mode))
{
isExist = true;
}
唯一不同的地方我使用了一个macro, S_ISDIR来判断文件是否存在,原理实际都一样的。
https://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=ZH-CN&k=k(%22WCHAR%2f_STAT%22);k(_STAT);k(DevLang-%22C%2B%2B%22);k(TargetOS-WINDOWS)&rd=true;k(_STAT);k(DevLang-%22C%2B%2B%22);k(TargetOS-WINDOWS)&rd=true)
boost的filesystem类库的exists函数
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/convenience.hpp>
int GetFilePath(std::string &strFilePath)
{
string strPath;
int nRes = 0;
//指定路径
strPath = "D:/myTest/Test1/Test2";
namespace fs = boost::filesystem;
//路径的可移植
fs::path full_path( fs::initial_path() );
full_path = fs::system_complete( fs::path(strPath, fs::native ) );
//判断各级子目录是否存在,不存在则需要创建
if ( !fs::exists( full_path ) )
{
// 创建多层子目录
bool bRet = fs::create_directories(full_path);
if (false == bRet)
{
return -1;
}
}
strFilePath = full_path.native_directory_string();
return 0;
}