943,915 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1445
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
May 14th, 2008
0

Iterator assertion failure. How?

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  1. while(iter_vsDirectory != v_sDirectories.end())
  2. {
  3. while(FindNextFile(hFind, &foundFileInfo))
  4. {
  5. cout << foundFileInfo.cFileName;
  6. if(foundFileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  7. {
  8. cout << " dir" << endl;
  9. if(s_currentDir.compare(foundFileInfo.cFileName) && s_parrentDir.compare(foundFileInfo.cFileName))
  10. {
  11. path = foundFileInfo.cFileName;
  12. path.append("\\*");
  13. v_sDirectories.push_back(path);
  14. cout << "Pushed back to dir vector: " << v_sDirectories.back() << endl;
  15. }
  16. }
  17. else
  18. {
  19. cout << " file";
  20. v_sFiles.push_back(foundFileInfo.cFileName);
  21. }
  22. cout << endl;
  23. }
  24. iter_vsDirectory++;
  25. }

In meantime I'll try make this code readable.
Last edited by Cybulski; May 14th, 2008 at 8:42 am.
Similar Threads
Reputation Points: 15
Solved Threads: 3
Light Poster
Cybulski is offline Offline
47 posts
since Apr 2008
May 14th, 2008
0

Re: Iterator assertion failure. How?

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.

C++ Syntax (Toggle Plain Text)
  1. HANDLE hFile = FindFirstFile( // blabla );
  2. if( hFile != INVALID_HANDLE_VALUE)
  3. {
  4. while( FindNextFile( // blabla ) )
  5. {
  6.  
  7. }
  8. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
May 14th, 2008
0

Re: Iterator assertion failure. How?

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:
C++ Syntax (Toggle Plain Text)
  1. d:\test\*
  2. iterator: d:\test\*
  3. .. dir
  4.  
  5. directory1 dir
  6. Pushed back to dir vector: directory1\*
  7.  
  8. directory2 dir
  9. Pushed back to dir vector: directory2\*
  10.  
  11. file1.txt file
  12. file2.txt file
Last edited by Ancient Dragon; May 14th, 2008 at 9:22 am. Reason: corrected quote tags
Reputation Points: 15
Solved Threads: 3
Light Poster
Cybulski is offline Offline
47 posts
since Apr 2008
May 14th, 2008
0

Re: Iterator assertion failure. How?

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.
Last edited by Ancient Dragon; May 14th, 2008 at 9:28 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
May 14th, 2008
0

Re: Iterator assertion failure. How?

Thank you very much mr. Ancient Dragon. Your code looks very nice and reusable. Will use these ideas in my solution.

[edit]
Anyway I'll think about it some more, inserting elements shouldn't invalidate iterator.
Last edited by Cybulski; May 14th, 2008 at 9:48 am.
Reputation Points: 15
Solved Threads: 3
Light Poster
Cybulski is offline Offline
47 posts
since Apr 2008
May 16th, 2008
0

Re: Iterator assertion failure. How?

Ok, here is my solution:

C++ Syntax (Toggle Plain Text)
  1. int DirectoryReader::MakeList(string p_sPath)
  2. {
  3. cout << "Parameter: " << p_sPath << endl;
  4. struct _finddatai64_t struct_filedata;
  5. string s_filename = p_sPath + "\\*.*";
  6. string s_current = ".";
  7. string s_parrent = "..";
  8.  
  9. cout << "Listing " << s_filename << endl;
  10.  
  11. long nHandle = _findfirsti64(s_filename.c_str(), &struct_filedata);
  12. if(nHandle >= 0)
  13. {
  14. while(_findnexti64(nHandle, &struct_filedata) == 0)
  15. {
  16. if(struct_filedata.attrib & _A_SUBDIR)
  17. {
  18. if( (s_current.compare(struct_filedata.name) && s_parrent.compare(struct_filedata.name)) )
  19. {
  20. s_filename = p_sPath + "\\" + struct_filedata.name;
  21. cout << "Subdir found: " << s_filename << endl;
  22. MakeList(s_filename);
  23. }
  24. else
  25. {
  26. cout << "Dir found: " << struct_filedata.name << endl;
  27. }
  28. }
  29. else
  30. {
  31. cout << "File found: " << struct_filedata.name << endl;
  32. m_vFilenames.push_back(struct_filedata.name);
  33. }
  34.  
  35. }
  36. }
  37. else
  38. {
  39. cout << "Path not found." << endl;
  40. return -1;
  41. }
  42.  
  43.  
  44. return 1;
  45. }

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 9:48 am.
Reputation Points: 15
Solved Threads: 3
Light Poster
Cybulski is offline Offline
47 posts
since Apr 2008
May 16th, 2008
0

Re: Iterator assertion failure. How?

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.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <io.h>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. vector<string> m_vFilenames;
  8. int MakeList(string p_sPath)
  9. {
  10. cout << "Parameter: " << p_sPath << endl;
  11. struct _finddatai64_t struct_filedata;
  12. string s_filename = p_sPath + "\\*.*";
  13. string s_current = ".";
  14. string s_parrent = "..";
  15.  
  16. cout << "Listing " << s_filename << endl;
  17.  
  18. long nHandle = _findfirsti64(s_filename.c_str(), &struct_filedata);
  19. if(nHandle >= 0)
  20. {
  21. while(_findnexti64(nHandle, &struct_filedata) == 0)
  22. {
  23. if(struct_filedata.attrib & _A_SUBDIR)
  24. {
  25. if( (s_current.compare(struct_filedata.name) && s_parrent.compare(struct_filedata.name)) )
  26. {
  27. s_filename = p_sPath + "\\" + struct_filedata.name;
  28. cout << "Subdir found: " << s_filename << endl;
  29. MakeList(s_filename);
  30. }
  31. else
  32. {
  33. cout << "Dir found: " << struct_filedata.name << endl;
  34. }
  35. }
  36. else
  37. {
  38. cout << "File found: " << struct_filedata.name << endl;
  39. m_vFilenames.push_back(struct_filedata.name);
  40. }
  41.  
  42. }
  43. }
  44. else
  45. {
  46. cout << "Path not found." << endl;
  47. return -1;
  48. }
  49.  
  50.  
  51. return 1;
  52. }
  53.  
  54. int main()
  55. {
  56.  
  57. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
May 16th, 2008
0

Re: Iterator assertion failure. How?

yes, it compiles and works ok, but this:
C++ Syntax (Toggle Plain Text)
  1. if( (s_current.compare(struct_filedata.name) && s_parrent.compare(struct_filedata.name)) )
  2. {
  3. s_filename = p_sPath + "\\" + struct_filedata.name;
  4. cout << "Subdir found: " << s_filename << endl;
  5. MakeList(s_filename);
  6. }
  7. else
  8. {
  9. cout << "Dir found: " << struct_filedata.name << endl;
  10. }

Should display:
C++ Syntax (Toggle Plain Text)
  1. Dir found: .
  2. Dir found: ..
for every directory. It only does it for "..". Why is that?

And I had to declare additional strings and use string.compare(...) because strcmp() takes no struct_filedata.name as parameter.
Reputation Points: 15
Solved Threads: 3
Light Poster
Cybulski is offline Offline
47 posts
since Apr 2008
May 16th, 2008
0

Re: Iterator assertion failure. How?

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
May 17th, 2008
0

Re: Iterator assertion failure. How?

>>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.
Reputation Points: 15
Solved Threads: 3
Light Poster
Cybulski is offline Offline
47 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: create blank charaters (white spaces)
Next Thread in C++ Forum Timeline: bubble sort





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC