?
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
Medium Priority
?
296 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 35

Accepted Solution

by:
sarabande earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.
Suggested Courses
Course of the Month9 days, 21 hours left to enroll

762 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