• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

Getting Names of files in C++

Hello

I require a code in C++ that will retrieve all the names of the files in the current directory from which it is executed.
   Example:
         D:\My docs\file 1.txt
         D:\My docs\file 2.txt

  It should return "file 1.txt" and "file 2.txt"

thank you
0
devguru001
Asked:
devguru001
  • 2
1 Solution
 
sarabandeCommented:
at windows you would use FindFirstFile and FindNextFile.

WIN32_FIND_DATA FindFileData;
HANDLE hFind = FindFirstFile("D:\\My Docs\\*.txt", &FindFileData);
if (hFind != INVALID_HANDLE_VALUE) 
{
     std::cout << "The first file found is " << FindFileData.cFileName << std::endl;
     ...

Open in new window


you would call FindNextFile in a loop to get more files and finally a FindClose to release the handle.

Sara
0
 
Infinity08Commented:
0
 
devguru001Author Commented:
Thanks alot Sara.... I was running the code under MS VS 2010 and got tons of errors.  Decided to instal Dev C++ and it works.... thanks again....
0
 
sarabandeCommented:
you should get solved the tons of errors by using project type 'win32 console application', switch-off precompiled header in the project settings and include <windows.h>. i assume you used 'managed c++' project which has some restrictions for using (unmanaged) c++ code.

but Dev C++ is a good choice either.

Sara
 
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now