C++ Vector problem

Jul 13, 2010 at 12:32pm
Hi all! I'm new in this forum, I got here after hours and hours struggling with a C++ which doesn't work as expected :-)
I have written a little function which checks all the files in a directory and writes their name in a .txt file, the problem is this:
the output only shows the LAST member added(for example the WINDOWS folder) repeted for the -n- number of the folders found( so if 10 files are found instead of showing "file1,file2,ecc.." it shows "WINDOWS,WINDOWS,WINDOWS" etc...
I've already checked and I don't have any problem with the stream itself!
Here is the code,I hope someone will hel me,I'm REALLY getting mad :(

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

void DriveExplore(char* path,char* chosenDir,fstream* theStream)
{
	WIN32_FIND_DATA findData;
	HANDLE hSearch;	
	vector<char*,allocator<char*>> folders;
	vector<char*,allocator<char*>>::iterator theIterator;
	*theStream<<"<r>"<<endl;
	*theStream<<chosenDir<<endl;
	strcat(path,"\\*");
	hSearch=FindFirstFile(path,&findData);

	do
	{
                 
		folders.push_back(findData.cFileName);

	}while(FindNextFile(hSearch,&findData));

	for(theIterator=folders.begin();theIterator!=folders.end();theIterator++)
	{
		*theStream<<*theIterator<<endl;
	}


}


I'm sure the problem is with the vector managing, but I can't understand how to solve :(
Since English is not my first language, I apologize if you don't understand something, in that case I'll try to explain better.
I'm using Visual C++ express 2010
Jul 13, 2010 at 1:11pm
It has nothing to do with vectors.
See here, this thread is about the same problem:
http://www.cplusplus.com/forum/general/24117/
Jul 13, 2010 at 1:59pm
The signature should really be:
void DriveExplore(const char* path, cibst char* chosenDir, fstream& theStream)

That would stop you from doing:
strcat(path,"\\*");

You don't check for FindFirstFile failure.

You don't close the find handle when you're done.

If you want to append the filename to the stream, you don't need to store it. You can add it to the stream in the loop.
e.g.
1
2
3
4
5
	do
	{
		(*theStream) << findData.cFileName << std::endl;
	}
	while(FindNextFile(hSearch,&findData));

Last edited on Jul 13, 2010 at 2:00pm
Jul 14, 2010 at 10:35pm
Thanks for the answers, I finally solved!
Topic archived. No new replies allowed.