Solved

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

Posted on 2009-04-08
5
2,399 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 500 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now