Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-04-08
5
Medium Priority
?
2,494 Views
Last Modified: 2013-12-14
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
Comment
Question by:CodingCat
  • 3
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 24103509
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
 

Author Comment

by:CodingCat
ID: 24103548
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
 
LVL 86

Expert Comment

by:jkr
ID: 24103555
What is the rest of your code in that function?
0
 

Author Comment

by:CodingCat
ID: 24103570
o, I put a finder.Close() in the loop which caused the problem.  Thanks.
0
 
LVL 86

Expert Comment

by:jkr
ID: 24103574
You're most welcome ;o)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question