C++ Vector problem

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
It has nothing to do with vectors.
See here, this thread is about the same problem:
http://www.cplusplus.com/forum/general/24117/
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
Thanks for the answers, I finally solved!
Topic archived. No new replies allowed.