• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2514
  • Last Modified:

how to list the files in a directory using CFileFind class?

hi,
I'm trying to list all the .jpg files using CFileFind class in a console program.  I put 3 .jpg files in the same directory for testing.
The program returns 3 32-bit integer, which looks like the addresses of something, but not the names of the jpg files.  what's wrong with the code?
CFileFind finder;
		BOOL bWorking = finder.FindFile(_T("*.jpg"), 0);
		bWorking ==0? printf("not find \n"): printf("found \n");
		while (bWorking)
		{
			bWorking = finder.FindNextFile();
			cout<<(LPCTSTR)(finder.GetFileName()) << endl;
			printf("\n"); }

Open in new window

0
CodingCat
Asked:
CodingCat
  • 3
  • 2
1 Solution
 
jkrCommented:
It seems that your porject settings are UNICODE, so you need to use 'wcout' instead, e.g.
CFileFind finder;
                BOOL bWorking = finder.FindFile(_T("*.jpg"), 0);
                bWorking ==0? printf("not find \n"): printf("found \n");
                while (bWorking)
                {
                        bWorking = finder.FindNextFile();
                        wcout<<(LPCTSTR)(finder.GetFileName()) << endl;  // <--- here
                        printf("\n"); }

Open in new window

0
 
CodingCatAuthor Commented:
the first .jpg file is displayed,  but an error window poped out immediately says
"Debug assertion Failed"  and I have to abort the program.
How to solve that?
0
 
jkrCommented:
What is the rest of your code in that function?
0
 
CodingCatAuthor Commented:
o, I put a finder.Close() in the loop which caused the problem.  Thanks.
0
 
jkrCommented:
You're most welcome ;o)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now