VerySource

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1155|回复: 6

请教,怎么得到一文件是否存在,用FileExists不行啊

[复制链接]

2

主题

2

帖子

2.00

积分

新手上路

Rank: 1

积分
2.00
发表于 2020-2-17 20:30:01 | 显示全部楼层 |阅读模式
请教各位大虾:
怎么得到一文件是否存在  ,用FileExists不行啊,有没有其他的函数?
回复

使用道具 举报

2

主题

19

帖子

11.00

积分

新手上路

Rank: 1

积分
11.00
发表于 2020-4-22 10:30:01 | 显示全部楼层
//给你一个自定义的函数来得到指定文件是否存在
bool FileExist(LPCTSTR szFindPath)
{
        int count = 0;
        int i = 0;
        WIN32_FIND_DATA fd;
        HANDLE hFind;
         if(strlen((char *)szFindPath) <= 0 )
                 return false;
        char path[257];
    char *pPath;
    pPath = path;
    count = strlen(szFindPath);
        if(count <=0)
                return false;
   

        //从前向后得到路径字符串
        for( i = 0; i<count; i++)
        {
        if(*(szFindPath+i) == '\\')
                        break;
                *(pPath+i) = *(szFindPath+i);
        }

   *(pPath+i) = '\\';
   *(pPath+i+1) = '\0';
    hFind = FindFirstFile(szFindPath, &fd );

        if ( hFind != INVALID_HANDLE_VALUE )
        {
                FindClose( hFind );
        }

        return hFind != INVALID_HANDLE_VALUE;
}
回复

使用道具 举报

0

主题

25

帖子

19.00

积分

新手上路

Rank: 1

积分
19.00
发表于 2020-4-23 12:45:01 | 显示全部楼层
(access(filename, F_OK)) == 0 )
回复

使用道具 举报

1

主题

7

帖子

8.00

积分

新手上路

Rank: 1

积分
8.00
发表于 2020-5-12 12:30:01 | 显示全部楼层
便例查找,与所需的文件名比较即可
CFileFind   ff ;
strPltfind = "*.*" ;
BOOL res = ff.FindFile(strPltfind);
while(res)   
{   
        res = ff.FindNextFile();   
        filename = ff.GetFilePath() ;
        //-----打开并存储文件数据---------------------
        //可以把filename存储起来
         if(!filename.Compare("/*要查找的文件名*/"))
         {
         ..................
         }
}
仅供参考:_)
回复

使用道具 举报

1

主题

5

帖子

6.00

积分

新手上路

Rank: 1

积分
6.00
发表于 2020-5-13 12:00:01 | 显示全部楼层
CString strTmp = "文件完整路径";
CFileFind   FileFind ;
BOOL br = FileFind .FindFile(strTmp );
if(br)
{
  //存在
}
else
{
  //不存在
}
回复

使用道具 举报

0

主题

13

帖子

7.00

积分

新手上路

Rank: 1

积分
7.00
发表于 2020-5-13 16:45:01 | 显示全部楼层
那有那么麻烦,用pathfileexists("c:\\a.txt");来判断就行了
回复

使用道具 举报

0

主题

5

帖子

6.00

积分

新手上路

Rank: 1

积分
6.00
发表于 2020-5-14 11:15:01 | 显示全部楼层
#include <io.h>

...
if(_access("c:\\path.ext",0) != -1)
    //文件存在
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

快速回复 返回顶部 返回列表