C/C++中如何判斷某一文件或目錄是否存在
來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次
1.C++很簡單的一種辦法:
復制代碼 代碼如下:
#include <iostream>
#include <fstream>
using namespace std;
#define FILENAME "stat.dat"
int main()
{
fstream _file;
_file.open(FILENAME,ios::in);
if(!_file)
{
cout<<FILENAME<<"沒有被創(chuàng)建";
}
else
{
cout<<FILENAME<<"已經(jīng)存在";
}
return 0;
}
2.利用 c 語言的庫的辦法:
函數(shù)名: access
功 能: 確定文件的訪問權限
用 法: int access(const char *filename, int amode);
以前一直沒用過這個函數(shù),今天調試程序發(fā)現(xiàn)了這個函數(shù),感覺挺好用,尤其是判斷一個文件或文件夾是否存在的時候,用不著再find了,文件的話還可以檢測讀寫權限,文件夾的話則只能判斷是否存在,下面摘自MSDN:
int _access( const char *path, int mode );
Return Value
Each of these functions returns 0 if the file has the given mode. The function returns –1 if the named file does not exist or is not accessible in the given mode; in this case, errno is set as follows:
EACCES
Access denied: file's permission setting does not allow specified access.
ENOENT
Filename or path not found.
Parameters
path
File or directory path
mode
Permission setting
Remarks
When used with files, the _access function determines whether the specified file exists and can be accessed as specified by the value of mode. When used with directories, _access determines only whether the specified directory exists; in Windows NT, all directories have read and write access.
mode Value Checks File For
00 Existence only
02 Write permission
04 Read permission
06 Read and write permission
Example
復制代碼 代碼如下:
/* ACCESS.C: This example uses _access to check the
* file named "ACCESS.C" to see if it exists and if
* writing is allowed.
*/
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
void main( void )
{
/* Check for existence */
if( (_access( "ACCESS.C", 0 )) != -1 )
{
printf( "File ACCESS.C exists " );
/* Check for write permission */
if( (_access( "ACCESS.C", 2 )) != -1 )
printf( "File ACCESS.C has write permission " );
}
}
OutputFile ACCESS.C existsFile ACCESS.C has write permission
3.在windows平臺下用API函數(shù)FindFirstFile(...):
(1)檢查文件是否存在:
復制代碼 代碼如下:
#define _WIN32_WINNT 0x0400
#include "windows.h"
int
main(int argc, char *argv[])
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
printf ("Target file is %s. ", argv[1]);
hFind = FindFirstFile(argv[1], &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);
}
return (0);
}
(2)檢查某一目錄是否存在:
復制代碼 代碼如下:
///目錄是否存在的檢查:
bool CheckFolderExist(const string &strPath)
{
WIN32_FIND_DATA wfd;
bool rValue = false;
HANDLE hFind = FindFirstFile(strPath.c_str(), &wfd);
if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
rValue = true;
}
FindClose(hFind);
return rValue;
}
4.使用boost的filesystem類庫的exists函數(shù)
復制代碼 代碼如下:
#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 ) );
//判斷各級子目錄是否存在,不存在則需要創(chuàng)建
if ( !fs::exists( full_path ) )
{
// 創(chuàng)建多層子目錄
bool bRet = fs::create_directories(full_path);
if (false == bRet)
{
return -1;
}
}
strFilePath = full_path.native_directory_string();
return 0;
}
欄 目:C語言
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/4299.html
您可能感興趣的文章
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對數(shù)怎么表達
- 04-02c語言沒有round函數(shù) round c語言
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 01-10如何判斷一個數(shù)是否為2的冪次方?若是,并判斷出來是多少次方
- 01-10深入理解C++中常見的關鍵字含義
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10如何判斷一個數(shù)是否為4的冪次方?若是,并判斷出來是多少次方
- 01-10如何查看進程實際的內存占用情況詳解
- 01-10深入Main函數(shù)中的參數(shù)argc,argv的使用詳解


閱讀排行
本欄相關
- 04-02c語言函數(shù)調用后清空內存 c語言調用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調用函數(shù)求fibo C語言調用函數(shù)求
隨機閱讀
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10C#中split用法實例總結
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實現(xiàn)頁面的局部加載