Microsoft Visual Studio: opendir() and readdir(), how?
Asked Answered
A

2

6

I've used this kind of code in my Dev-cpp before:

if((dh = opendir(folder)) !== false){
    while((file = readdir(dh)) !== false){
        // do my stuff
    }
    closedir(dh);
}

But now i am using MSVC++ and i dont know how to add those files there, i tried to copy dirent.h/dir.h/errno.h in there, but it gives another error relating to another included files inside those files ..., and by looking in the files i see mingw stuff there so its compiler related? idk what compiler MSVC++ uses, but is it possible to copypaste those files in MSVC++ and get it working?

I tried to look up some code from MSDN but it was really messed up, so im hoping i could use these functions above...

Accumulation answered 19/5, 2009 at 15:54 Comment(2)
Isn't that PHP code? This comparison operator is not available in C.Danita
lol yeah i noticed that too, but the C version is quite the same anyways...Accumulation
A
5

I would suggest using FindFirstFile() and FindNextFile().

sample code:

HANDLE hFind;
WIN32_FIND_DATA FindFileData;

if((hFind = FindFirstFile("C:/some/folder/*.txt", &FindFileData)) != INVALID_HANDLE_VALUE){
    do{
        printf("%s\n", FindFileData.cFileName);
    }while(FindNextFile(hFind, &FindFileData));
    FindClose(hFind);
}

This really is better, because i can use "*.txt" etc, makes it much more easier to find some specific filetypes, earlier i had to write own match function for that :D

Accumulation answered 19/5, 2009 at 20:37 Comment(0)
R
1

Use boost::filesystem, or std::filesystem if you are using C++17

Randazzo answered 18/8, 2019 at 10:1 Comment(2)
that answer would be worth being developped as c++ 17 brings this service in standard. Adding some code would be welcomeQuintus
Easy enough to lookup.Obviate

© 2022 - 2024 — McMap. All rights reserved.