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
291 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
1 Comment
 
LVL 33

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

813 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

12 Experts available now in Live!

Get 1:1 Help Now