Solved

Finding Files

Posted on 2004-04-23
7
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

707 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