以下经过调试成功的方法:
CFileFind fileFinder;
BOOL bWorking = fileFinder.FindFile("sample.txt");
while (bWorking)
{
bWorking = fileFinder.FindNextFile(); if (!fileFinder.IsDirectory () && !fileFinder.IsSystem () &&
!fileFinder.IsHidden () && !fileFinder.IsTemporary ())
{
// the file "sample.txt" is found in current dir.
}
}
fileFinder.Close();
将samlpe.txt改成你的文件名就OK
CFileFind fileFinder;
BOOL bWorking = fileFinder.FindFile("sample.txt");
while (bWorking)
{
bWorking = fileFinder.FindNextFile(); if (!fileFinder.IsDirectory () && !fileFinder.IsSystem () &&
!fileFinder.IsHidden () && !fileFinder.IsTemporary ())
{
// the file "sample.txt" is found in current dir.
}
}
fileFinder.Close();
将samlpe.txt改成你的文件名就OK
用尝试打开的方法不就行了???如果在当前目录下能打开,文件就在呗。
打开不成功,自然就不存在。没有必要去CFileFind
CFile file;
if(file.open(filename,CFile::modeRead))
cout<<"the specified file is exist"<<endl;
else
cout<<"the specified file is not exist"<<endl;
2.
HANDLE hfile; WIN32_FIND_DATA fd;
if((hfile=FindFirstFile(colorstring,&fd))==INVALID_HANDLE_VALUE)
cout<<"the specified file is exist"<<endl;
else
cout<<"the specified file is not exist"<<endl;
我试过你们的方法,都可以,但我发现
1. CFileFind fileFinder;
BOOL bWorking = fileFinder.FindFile("*.*");
while (bWorking)
{
bWorking = fileFinder.FindNextFile();
CString filename=fileFinder.GetFileName();
to billbord(billy) and nhcf() :
我试过你们的方法都可以,但当我用如下两方法想得到某目录中所有文件时,却发现
前两次循环得到的文件名都为:"."和".." ,请问怎么回事。
1. CFileFind fileFinder;
BOOL bWorking = fileFinder.FindFile("*.*");
while (bWorking)
{
bWorking = fileFinder.FindNextFile();
CString filename=fileFinder.GetFileName();
}
2
HANDLE hfile; WIN32_FIND_DATA fd;
CString folder,curdirfile;
GetCurrentDirectory(folder);
curdirfile=folder+"*.*";
hfile=FindFirstFile(curdirfile,&fd);
if((hfile!=INVALID_HANDLE_VALUE)&&(bfind==TRUE))
{
CString filename=fd.cfilename;
bfind=FindNextFile(hfile,&fd);
}
else
cout<<"the specified file is not exist"<<endl;
“.”代表该级目录
“..”代表上级目录
在DOS里不就有了吗
正确的操作方法应该加上如下判断:
//------------------------------------------------------------
// start to search all the file under the given directory
//------------------------------------------------------------
CFileFind fileFinder;
BOOL bWorking = fileFinder.FindFile("*.*");
while (bWorking)
{
bWorking = fileFinder.FindNextFile(); if (!fileFinder.IsDirectory () && !fileFinder.IsSystem () &&
!fileFinder.IsHidden () && !fileFinder.IsTemporary ())
{
找到的文件名:fileFinder.GetFileTitle ()
}
}fileFinder.Close();