Solved

how do you list all the txt files from a folder using c++ in a very SIMPLE manner

Posted on 2011-03-13
1
295 Views
Last Modified: 2012-05-11
Hi, what function and header do you have to use to list all the txt files from a folder.

could you also give a very simple SAMPLE code?

The code should not be more 25 lines.

Thanks again.
0
Comment
Question by:pgmerLA
[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
1 Comment
 
LVL 34

Accepted Solution

by:
sarabande earned 500 total points
ID: 35123539
I thank all for this wise solution:

below is the 'simple code' commented.

Sara


#include <iostream>
#include <windows.h>

int main () 
{
   // defines the stucture for find file at WINAPI
   WIN32_FIND_DATA fd;

   // the first call may fail and return INVALID_HANDLE_VALUE
   // what is -1 casted to a HANDLE
   HANDLE hFind = FindFirstFile("c:\\path\\*.txt"),&fd);
   // in this case GetLastError will give a specific error 
   // check winerror.h for further analysis
   // error 2 means file not found
   // error 3 means folder not found
   // error 5 means access denied
   if (hFind == INVALID_HANDLE_VALUE)
      return GetLastError();

   do 
   {
      // you can collect the filenames here in the loop 
      // directories would have the directory flag as attribute 
      if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
         std::cout << "File: " << fd.cFileName << std::endl;
   } 
   while (FindNextFile(hFind, &fd) != NULL);
   // the loop should end with 'no more files'
   if (GetLastError() != ERROR_NO_MORE_FILES)
      return GetLastError();
   FindClose(hFind);
   return 0;
}

Open in new window

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

688 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