My guess is that neither of those loops will work.
1) how is iter_vsDirectory initialized? You failed to post it.
2) what is the purpose of that first loop with the iterator ? The iterator doesn't appear to be used anywhere within the loop. Adding more strings to the end of the vector might invalidate the iterator.
3) You have to call FindFirstFile() before you can start a loop for FindNextFile(). The FindNextFile() you have code will always fail.
HANDLE hFile = FindFirstFile( // blabla );
if( hFile != INVALID_HANDLE_VALUE)
{
while( FindNextFile( // blabla ) )
{
}
}
Ancient Dragon
Retired & Loving It
30,042 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
From what you said in 2) you apparently did not post all the code. If that is correct then I suspect the problem is the iterator is getting invalidated when a new string is put into the vector.
If you are attempting to build a list of directory and their subdirectory names then maybe using recursion would be a better approach. Here is an example program how to do that. It gets all the file names as well, but you can easily modify the code to do what you want with it.
Ancient Dragon
Retired & Loving It
30,042 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
you must have something else wrong because your function compiled ok for me. All I did was make it a simple function and declare the vector globally.
#include <iostream>
#include <string>
#include <io.h>
#include <vector>
using namespace std;
vector<string> m_vFilenames;
int MakeList(string p_sPath)
{
cout << "Parameter: " << p_sPath << endl;
struct _finddatai64_t struct_filedata;
string s_filename = p_sPath + "\\*.*";
string s_current = ".";
string s_parrent = "..";
cout << "Listing " << s_filename << endl;
long nHandle = _findfirsti64(s_filename.c_str(), &struct_filedata);
if(nHandle >= 0)
{
while(_findnexti64(nHandle, &struct_filedata) == 0)
{
if(struct_filedata.attrib & _A_SUBDIR)
{
if( (s_current.compare(struct_filedata.name) && s_parrent.compare(struct_filedata.name)) )
{
s_filename = p_sPath + "\\" + struct_filedata.name;
cout << "Subdir found: " << s_filename << endl;
MakeList(s_filename);
}
else
{
cout << "Dir found: " << struct_filedata.name << endl;
}
}
else
{
cout << "File found: " << struct_filedata.name << endl;
m_vFilenames.push_back(struct_filedata.name);
}
}
}
else
{
cout << "Path not found." << endl;
return -1;
}
return 1;
}
int main()
{
}
Ancient Dragon
Retired & Loving It
30,042 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
>>if( (s_current.compare(struct_filedata.name) && s_parrent.compare(struct_filedata.name)) )
use || operator instead of && because it can't be both at the same time.
Ancient Dragon
Retired & Loving It
30,042 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
"." should be found and displayed, but it isn't.
You are missing the initial match ...
...
long nHandle = _findfirsti64(s_filename.c_str(), &struct_filedata);
if(nHandle >= 0)
{
<strong>cout << "First match is: " << struct_filedata.name << '\n';</strong>
while(_findnexti64(nHandle, &struct_filedata) == 0)
{
...
}
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
If you mean _findfirsti46(), then no. It isn't counted as first result.
I disagree, MSDN says ...
_findfirst, _findfirsti64, _wfindfirst, _wfindfirsti64
Providesinformation about the first instance of a filename that matches the file specified in the filespec argument.
http://msdn.microsoft.com/en-us/library/aa246869(VS.60).aspx
So, the function takes the struct _finddatai64_t * as input for a good reason.
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395