•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 392,045 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,306 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 525 | Replies: 12
![]() |
Hello everyone. Welcome to my first post.
I have problem, it didn't brough me here, but maybe someone figure it out, I'm staring at this piece of code for hour. I'm trying to make directory listing class. It puts filenames and directories to vectors. It's not finished, yet not working loop:
In meantime I'll try make this code readable.
I have problem, it didn't brough me here, but maybe someone figure it out, I'm staring at this piece of code for hour. I'm trying to make directory listing class. It puts filenames and directories to vectors. It's not finished, yet not working loop:
while(iter_vsDirectory != v_sDirectories.end())
{
while(FindNextFile(hFind, &foundFileInfo))
{
cout << foundFileInfo.cFileName;
if(foundFileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
cout << " dir" << endl;
if(s_currentDir.compare(foundFileInfo.cFileName) && s_parrentDir.compare(foundFileInfo.cFileName))
{
path = foundFileInfo.cFileName;
path.append("\\*");
v_sDirectories.push_back(path);
cout << "Pushed back to dir vector: " << v_sDirectories.back() << endl;
}
}
else
{
cout << " file";
v_sFiles.push_back(foundFileInfo.cFileName);
}
cout << endl;
}
iter_vsDirectory++;
}
In meantime I'll try make this code readable.
Last edited by Cybulski : May 14th, 2008 at 7:42 am.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,561
Reputation:
Rep Power: 36
Solved Threads: 860
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.
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 ) )
{
}
} I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
•
•
•
•
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.
1)
vector<string>::iterator iter_vsDirectory = v_sDirectories.begin();
2)
First loop is meant cycle trough directories. For each directory found in v_sDirectories it will call FindFirstFile, then internal loop to list files/directories inside. In case of dir found, it will append last setion of path to work recirsive in all directories inside one given at start.
If adding objects to vector invalidates iterator - thats not the way vector should work imo.
3)
It is called, before main loop.
There is result of running it:
d:\test\* iterator: d:\test\* .. dir directory1 dir Pushed back to dir vector: directory1\* directory2 dir Pushed back to dir vector: directory2\* file1.txt file file2.txt file
Last edited by Ancient Dragon : May 14th, 2008 at 8:22 am. Reason: corrected quote tags
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,561
Reputation:
Rep Power: 36
Solved Threads: 860
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.
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.
Last edited by Ancient Dragon : May 14th, 2008 at 8:28 am.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Ok, here is my solution:
Two things bother me:
1) strcmp() don't want take data.name as first parameter,
2) "." should be found and displayed, but it isn't.
It works well besides that.
cplusplus Syntax (Toggle Plain Text)
int DirectoryReader::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; }
Two things bother me:
1) strcmp() don't want take data.name as first parameter,
2) "." should be found and displayed, but it isn't.
It works well besides that.
Last edited by Cybulski : May 16th, 2008 at 8:48 am.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,561
Reputation:
Rep Power: 36
Solved Threads: 860
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()
{
} I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
yes, it compiles and works ok, but this:
Should display:
for every directory. It only does it for "..". Why is that?
And I had to declare additional strings and use
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;
}Should display:
Dir found: . Dir found: ..
And I had to declare additional strings and use
string.compare(...) because strcmp() takes no struct_filedata.name as parameter. •
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,561
Reputation:
Rep Power: 36
Solved Threads: 860
>>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.
use || operator instead of && because it can't be both at the same time.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
•
•
•
•
>>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.
Nope, not working. Negating whole condition not working also. Only way to get list of files is using && operator, and my brain is melting while thinking why.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- Debug Assertion Failure (Web Browsers)
Other Threads in the C++ Forum
- Previous Thread: create blank charaters (white spaces)
- Next Thread: bubble sort



Linear Mode