If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Capturing files in a directory

Hi, I'm using the following code to capture all files in a directory -
but when I pass the "path" var such as "C:\SomeDir" ffd.cFileName is just simply "SomeDir" and is seen as a valid folder and no more files are processed.. I'm trying to get the contents in that folder. Where am I going wrong? I'm following this example here -http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

Re: Capturing files in a directory

I tried using a reference instead of a static var for my files vector like so -

Code:

bool ListFiles(TCHAR* path, std::vector<TCHAR *> &files) {

but when I use it, I have a bunch of valid file names after the ListFiles call, but a few lines later, all the values are junk such as "iiiiiiiiiiiiiiiiiiiiiii", what could cause it?

First, what is "textArea"?

Second, you changed "ListFiles". Don't you think it makes a difference we see what this new ListFiles() consists of.

Third, your code has a lot wrong with it.

1) It has memory leaks. You call "new[]" and nowhere do you call "delete[]" to release the memory.

2) Why did you even introduce "new[]" in the first place, while you're using vector? A vector is supposed to relieve you from using new[]/delete[] since it is a dynamic array container, but you're not using vector for the purpose for which it was intended for. If you're using std::vector, but at the same time using new[]/delete[], then it's time to take a step back and look carefully at what you're doing, since somewhere you're making a design mistake, misstep, or oversight (unless you're a C++ pro and you have a good reason to be using both).

Things like this:

Code:

TCHAR* text = new TCHAR[_MAX_PATH];

are totally unnecessary. Not only that, you're doing this in a loop and creating huge memory leaks without a call to delete[].

Moreover, why even use dynamically allocated memory anyway, when you know the size of the array? It isn't even dynamic. So you're needlessly slowing your code down by calling the allocator and you're creating a memory leak with the code you have now.

Code:

TCHAR text[_MAX_PATH];

All you need is a TCHAR array that has _MAX_PATH elements. You make this mistake in this new code and the original code you have at the top of the thread.

If _MAX_PATH were some huge number into the millions, then maybe that would be reason to allocate dynamically. But since _MAX_PATH is something very small, just create a normal, non-dynamic array of _MAX_PATH size. Even if you need to allocate dynamically, std::vector<> should have been used instead of new[].

Since it looks like you're using Visual C++, you could have just used CString (if VC++ is higher than 6.0, since CString is not tied to MFC in higher versions of VC++), or a level down from that, std::string/std::wstring and choose the right one depending on build type (Unicode/ANSI). Then replace all of those TCHAR* with CString or std::(w)string.

The bottom line being that you shouldn't need to write code that ties you up in knots with respect to memory allocation. The code should be immune to memory leaks even if an exception is thrown, and using things such as CString/std::(wstring) ensures this due to the destructors of these classes cleaning up themselves automatically.

Re: Capturing files in a directory

yes, sorry. I'm just trying to understand TCHAR and it's correct usage for the most part. Is it possible to make a vector containing a TCHAR[] array or is that just a no no? I tried Paul's StringClass suggestion, but how do I convert a StringVector to a LPSTR when needed?

Re: Capturing files in a directory

yes, sorry. I'm just trying to understand TCHAR and it's correct usage for the most part. Is it possible to make a vector containing a TCHAR[] array

Where in your code do you need an LPSTR? All I see in your code is to gather the filenames.

Secondly, StringVector is a vector of CString/std::(wstring). It isn't just one string, so there is no such thing as converting a StringVector to an LPSTR. What is possible is to take one of the stirngs in the StringVector and return an LPCTSTR -- that's why I asked why you need LPSTR instead of LPCTSTR.