[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
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
?
298 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

656 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