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
289 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 32

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

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…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

744 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

11 Experts available now in Live!

Get 1:1 Help Now