Solved

Finding Files

Posted on 2004-04-23
7
177 Views
Last Modified: 2010-04-01

My app is created with vc++ 6.0 win32 with MFC in a shared Dll.

I'm looking for an example code to look for files (ex:c:\test\*.txt -- look for all 'txt' files) and store them in an array and loop through the array.

Thanks.
0
Comment
Question by:newuser11
  • 2
7 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 10901025
0
 

Author Comment

by:newuser11
ID: 10901497
I have tried the CFileFind. Thanks. I have saved the files in a LPCTSTR array. When I tried to loop through, it's not displaying the files. what's wrong with the displaying?

  CFileFind finder;
 LPCTSTR  szFileNames[255];
int i=0;
   BOOL bWorking = finder.FindFile("c:\test\*.txt");

   while (bWorking)
   {
      bWorking = finder.FindNextFile();
     szFileNames[i] =  finder.GetFileName();

      cout << "File Name " << szFileNames[i] << endl;  
     i++;
   }

  for (int j=0; szFileNames[j] != NULL; j++)
  {
      cout << "For Loop " <<  &szFileNames[0] <<  endl;
  }

Thanks.
0
 

Author Comment

by:newuser11
ID: 10901867
Increased points
0
 
LVL 14

Accepted Solution

by:
wayside earned 200 total points
ID: 10902073
> for (int j=0; szFileNames[j] != NULL; j++)
>   {
>      cout << "For Loop " <<  &szFileNames[0] <<  endl;
>   }

Try

for (int j=0; szFileNames[j] != NULL; j++)
  {
     cout << "For Loop " << szFileNames[j] <<  endl;
  }


Also:

> LPCTSTR  szFileNames[255];

You didn't initialize this anywhere, but you are depending on an element value to be NULL to terminate the loop. To initialize:

LPCTSTR  szFileNames[255] = { NULL };

Also:

>    szFileNames[i] =  finder.GetFileName();

What if i > 255? You've overwritten the end of your array and will most likely crash.


0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

759 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

13 Experts available now in Live!

Get 1:1 Help Now