C/C++中判断一个文件是否存在的方法

  • 判断一个文件是否存在的几种方式:

1.  利用CFileFind类;

2. 利用::FindFirstFile函数;

3. 利用PathFileExists函数,注意要包含头文件及库:

#include "shlwapi.h"

#pragma comment( lib, "shlwapi.lib")

4. 利用openfile或createfile函数,如果失败,则可以判断。

 

5. 自己实现,其实也是利用上面的函数


inline BOOL CSearchThread::_FileIsExist(TCHAR*   pszFilePath) 

    WIN32_FIND_DATA   FindFileData; 
    HANDLE  hFind; 

    hFind = FindFirstFile(pszFilePath,&FindFileData); 

    return (hFind == INVALID_HANDLE_VALUE?FALSE:FindClose(hFind)); 
}  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/** ===================================================== 功能: CFolderContent类搜索一个目录, 列出该目录下的所有目录名称,列出所有子目录下的文件名称等属性。 作者: jef 作者邮箱: [email protected] 发步时间: 20100311 版本: v1.1 版权: 请遵循GNU. 对外接口: int GetAllSub(CString csPath) csPath: 一个目录或者一个完整的文件名 使用举例: void CGetFolderContentView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CString cs,csSaveFileName,csFileSave,csT,csT1; cs ="C:\\Documents and Settings\\All Users\\Documents\\My Music"; char chModule[8192]; memset(chModule,0,8192); GetModuleFileName(NULL,chModule,8192); csT.Format("%s",chModule); csT =csT.Left( csT.ReverseFind(_T('\\')) ); csSaveFileName =csT+_T("\\FolderContent输出文件.txt"); CRect rtClient; GetClientRect(&rtClient); CClientDC dc(this); dc.SetTextColor(RGB(200,80,80)); dc.FillSolidRect(rtClient,RGB(240,240,240)); CFolderContent fc; fc.GetAllSub(cs); int i,len,k; csT ="-------"; csT =cs+"总大小为"; csT1.Format(" %.4fMB,清单文件已保存在 %s",fc.m_dTotalSize/double(1024*1024),csSaveFileName); csT +=csT1; csT +="-------"; dc.TextOut(5,0,csT); csFileSave +=csT+_T("\r\n"); csT ="-------"; csT +="所有子目录如下:"; csT +="-------"; dc.TextOut(5,25,csT); csFileSave +=csT+_T("\r\n"); len =fc.m_Directory.GetSize(); for (i=0;i<len;i++) { csT.Format(" 大小:%.4fMB",fc.m_vecDirectorySize[i]/double(1024*1024)); csT1.Format(" 创建时间:%d年%d月%d日 ", fc.m_vecFileLastCreationTime[i].wYear, fc.m_vecFileLastCreationTime[i].wMonth, fc.m_vecFileLastCreationTime[i].wDay); csT =csT+csT1; csT =fc.m_Directory.GetAt(i)+csT; dc.TextOut(5,(i+2)*20,csT); csFileSave +=csT+_T("\r\n"); } k =i; csT ="-------"; csT +="所有文件如下:"; csT +="-------"; dc.TextOut(5,(k+3)*20,csT); csFileSave +=csT+_T("\r\n"); len =fc.m_PathNameExt.GetSize(); for (i=0;i<len;i++) { csT.Format(" 大小:%.4fMB",fc.m_vecFileSize[i]/double(1024*1024)); csT1.Format(" 修改时间:%d年%d月%d日 ", fc.m_vecFileLastWriteTime[i].wYear, fc.m_vecFile

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值