Chinaunix
标题:
win7下FindFirstFile 获取句柄 返回 5(拒绝访问),求助[自己解决了]
[打印本页]
作者:
Anleb
时间:
2013-07-05 10:05
标题:
win7下FindFirstFile 获取句柄 返回 5(拒绝访问),求助[自己解决了]
本帖最后由 Anleb 于 2013-07-05 15:22 编辑
环境:win7 64位
语言:C
编译:GCC
编译结果: 成功
执行结果:有时候成功,有时候不成功
程序目的:本程序是让用户输入一个目录,删除用户自定义后缀名的所有文件。
问题:
void findAllFile(char *pFilePath,char *extName)
{
WIN32_FIND_DATA FindFileData;
DWORD dwError;
HANDLE hFind = INVALID_HANDLE_VALUE;
char DirSpec[MAX_PATH+1];
strncpy(DirSpec, pFilePath, strlen(pFilePath) + 1);
SetCurrentDirectory(pFilePath);
strncat(DirSpec, "\\*", 3);
hFind = FindFirstFile(DirSpec, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE){
printf ("Invalid file handle. Error is %u\n", GetLastError());
return ;
}
}
复制代码
当我尝试打开 C盘(系统盘)时,就会返回 GetLastError() = 5,当我尝试在其他磁盘获取句柄时,没有问题,我怀疑是 权限问题,
请大家帮看看
注意:当我尝试在方法开头 printf(pFilePath); 问题 居然不会出现了,很诡异。
附上 源码,希望大家帮助我。
#include <stdio.h>
#include <direct.h> //_getcwd(), _chdir()
#include <stdlib.h> //_MAX_PATH, system()
#include <io.h> //_finddata_t, _findfirst(), _findnext(), _findclose()
#include <string.h>
#include <windows.h>
//获取后缀名
char *substr(const char*str)
{
char *ptr, c = '.';
static char stbuf[256];
ptr = strrchr(str, c); //最后一个出现c的位置
if(ptr == NULL){
return stbuf;
}
int pos = ptr-str;//用指针相减 求得索引
unsigned start = pos + 1;
unsigned end = strlen(str);
unsigned n = end - start;
strncpy(stbuf, str + start, n);
stbuf[n] = 0; //字串最后加上0
return stbuf;
}
//递归查询文件并且删除
void findAllFile(char *pFilePath,char *extName)
{
WIN32_FIND_DATA FindFileData;
DWORD dwError;
HANDLE hFind = INVALID_HANDLE_VALUE;
char DirSpec[MAX_PATH+1];
strncpy(DirSpec, pFilePath, strlen(pFilePath) + 1);
SetCurrentDirectory(pFilePath);
strncat(DirSpec, "\\*", 3);
hFind = FindFirstFile(DirSpec, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE){
printf ("Invalid file handle. Error is %u\n", GetLastError());
return ;
}
else{
if (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY ){
// printf ("%s\n", FindFileData.cFileName);
}
else if(FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY&& strcmp(FindFileData.cFileName, ".") != 0&& strcmp(FindFileData.cFileName, "..") != 0){
char Dir[MAX_PATH + 1];
strcpy(Dir, pFilePath);
strncat(Dir, "\\", 2);
strcat(Dir, FindFileData.cFileName);
findAllFile(Dir,extName);
}
while (FindNextFile(hFind, &FindFileData) != 0){
if (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY){
char *extname2 = substr(FindFileData.cFileName);
if(strcmp(extname2,extName) ==0){
printf ("FileName:%s Delete:True\n", FindFileData.cFileName);
remove(FindFileData.cFileName);
}
}
else if(FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY && strcmp(FindFileData.cFileName, ".") != 0&& strcmp(FindFileData.cFileName, "..") != 0){
char Dir[MAX_PATH + 1];
strcpy(Dir, pFilePath);
strncat(Dir, "\\", 2);
strcat(Dir, FindFileData.cFileName);
findAllFile(Dir,extName);
}
}
dwError = GetLastError();
FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES) {
printf ("FindNextFile error. Error is %u\n", dwError);
return;
}
}
}
//开始显示部分
void Show(char str[])
{
int i,len;
len = strlen(str);
for(i=0;i<len;i++)
{
printf("%c",str[i]);
sleep(100);
}
}
int main(void)
{
printf("Anleb : ");
sleep(1000);
char string1[] = "I am Anleb,nice to somthing!\n";
Show(string1);
printf("Anleb : ");
sleep(1000);
char string2[] = "Go,gay!\n";
Show(string2);
printf("Please Enter the Path:");
char path[128];
gets(path);
printf("Please Enter the ExtName:");
char extName[10];
gets(extName);
findAllFile(path,extName);
system("pause");
return 0;
}
复制代码
作者:
bruceteen
时间:
2013-07-05 13:25
太缺德了,我运行了一下,把文件都删除掉了
作者:
bruceteen
时间:
2013-07-05 13:25
太缺德了,我运行了一下,把文件都删除掉了
作者:
Anleb
时间:
2013-07-05 13:33
回复
3#
bruceteen
大哥啊,我已经说明了 我上面程序的目的,你怎么不当心呢。。。
作者:
bruceteen
时间:
2013-07-05 13:37
想想就生气,既然你问的是 FindFirstFile 的问题,你就应该将无关的 remove(FindFileData.cFileName) 等删除掉,简化一下代码,只留下相关的代码。
既然你留着 remove(FindFileData.cFileName),那你就应该在一开始的时候,特别注明一下,提醒别人小心。
我好心帮你调试一下,你却害得我将C盘中exe文件删除掉了,现在系统运行一会儿就报错,我都不知道该怎么办才好。真是好心反而被人咬!
作者:
Anleb
时间:
2013-07-05 15:21
回复
5#
bruceteen
....
不好意思啊 大哥。。。
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2