Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

qwertybob

getting list of files in a directory

This topic is 5653 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Guest Anonymous Poster
cheers

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
hi, im having a little problem, and i cant figure out what im doing wrong, here is moy code and output.


#include <iostream>
#include <windows.h>
#include <list>

using namespace std;

int main()
{
std::list<LPCTSTR> files;

WIN32_FIND_DATA w32fd;
HANDLE hFind;

hFind = FindFirstFile("c:\\a\\*.txt" , &w32fd);
files.push_back(w32fd.cFileName);
cout << w32fd.cFileName <<endl;


while ( FindNextFile ( hFind, &w32fd))
{
files.push_back(w32fd.cFileName);
cout << w32fd.cFileName <<endl;
}
FindClose ( hFind);

cout << endl << endl;

while (files.size() > 0)
{
cout << files.front();
files.pop_front();
cout << endl;
}

return 0;
}


and the output

1.txt
2.txt
3.txt

3.txt
3.txt
3.txt

i will be greatfull if someone can tell me what im doing wrong, thanx

Share this post


Link to post
Share on other sites
You''ve set all the elements in the list to point to the data of w32fd.cFileName, so the same text is being printed repeatedly.
Instead, you need to make copies of the strings. #include <string>, and change the declaration of files to:

std::list<std::string> files;

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
ah i feel stupid now thanx.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!