File listing problem

Hi all, I've written a little function which looks for all directories,files and subdirectories in the whole system(assuming you pass it the parameter "C:")
The problem is this: after having listed the files contained in C:, the first directory and its files, it should come back and processing the next folder,BUT an error occurs!
Unhandled exception:0xC0000005: Access violation
Then the compiler redirects me at dbgheap.c file, line 1431 :(
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
void DriveExplore(char* path,const char* chosenDir,fstream* theStream,bool listFiles)
{
	
	WIN32_FIND_DATA findData;
	HANDLE hSearch;

	if(listFiles)
	{
	
	*theStream<<"<r>"<<endl;
	*theStream<<chosenDir<<endl;
	*theStream<<"<f>"<<endl;
	
	}
	strcat(path,"\\*.*");

	hSearch=FindFirstFile(path,&findData);
	if(hSearch!=INVALID_HANDLE_VALUE)
	{
	do
	{
		if(listFiles==false)
		{

		
		if(findData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
		{

		
		   RemoveEndString(path,"\\*.*");
		   

		   char slashs[2]="\\";
		  
		   if(!StartsWith(findData.cFileName,"."))
		   {
			chosenDir=findData.cFileName;
		       cout<<chosenDir<<endl;
		       strcat(slashs,chosenDir);
		       strcat(path,slashs);
		       //cout<<"Current path :"<<path<<endl;
		       DriveExplore(path,chosenDir,theStream,true);
		   
		   }
		   else
		   {
			  
			   break;
		   }
		}

		}
		else //if(listFiles==true)
		{
			                                     if(!(findData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
			{
				*theStream<<findData.cFileName<<endl;

			}

		}
	
	}while(FindNextFile(hSearch,&findData));
	}
	if(listFiles==true)
	{
	*theStream<<"</f>"<<endl;
	RemoveEndString(path,"\\*.*");
	DriveExplore(path,chosenDir,theStream,false);
	}
	else if(listFiles==false);
	{
		*theStream<<"</r>"<<endl;

	}
}



I noticed, by debugging, that the error occurs in the bolded line of code
while(FindNextFile(hSearch,&findData));

How could I solve?
Thanks in advance!
We've seen this before right?
http://www.cplusplus.com/forum/beginner/26077/#msg138853

You've not taken my advice about changing the prototype, and thus haven't dealt with the possible buffer overrun you do with strcat.

You're treatment of path is bad. If you want to create a recursive function, it must take a copy of the things it's using. You've passed in path, but it keeps getting modified by each recursive call, so it's lost context.

Also, directories you ignore are . and .. Just because a file starts with . doesn't mean it's to be ignored.
First of all, I changed the char* in const char* as you suggested in ther other topic, but it gives me a conversion error :(
About the fact that path loses context, can you give me some suggestions about how to solve?(I DON'T want the full code, just some little tips to solve by myself :p)

The program should work in this way:
Looks for all files in C:
Recursive call with the same path
Now looks for the first directory
Looks for files
If there are not subdirs it SHOULD come back to the root directory and doing the same with the next folder using the FindNextFile function.
I've never used recursive functions before!

Why, in your opinion, does the access violation occur in the "while" instruction? Compilers says that there's a reading access violation in a address which is about the same of the "hSearch" one, it just differs in the last due numbers.
path should be const char*, so you're clear that it's read-only.

Instead of amending the path you pass in, use it to construct the local strings that you need.

e.g.
1
2
    std::string localpath = path;
    std::string searchpath = localpath + "\\*.*";


Then, for each directory, you can construct a new path to be passed to the recursive call with something like:
std::string newpath = localpath + std::string("\\") + findData.cFileName;
Last edited on
Topic archived. No new replies allowed.