Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2540
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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