123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|431|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> Off Topic -> ReadDir for C

Sat, 06 Jan 2018, 19:44
jm2bits
C++ doesn't have a ’’Dir’’/Folder List function!?

There is Dirent - which is a POSIX thing (so will work on anything not windows) and a Windows compatibility here: Linkage
Sat, 06 Jan 2018, 19:44
steve_ancell
There are ways, but it's all arse about tit. www.dummies.com/programming/cpp/how-to-get-the-contents-of-a-directory-in-c/
Sun, 07 Jan 2018, 04:18
Jayenkai
I’m sure there's 100 ways to "add it", I was just moaning that, even though it can seemingly pull off 100,000 File Access methods itself, reading a directory isn’t one of them, so it's a pain in the arse to actually GET to the files in the first place.
Whoever decided that was a good idea, deserves a swift kick up the backside.

-=-=-
''Load, Next List!''
Sun, 07 Jan 2018, 04:50
jm2bits
From: https://stackoverflow.com/questions/9886233/how-to-get-a-list-of-files-using-only-the-c-standard-library/9886902#9886902

"There is no portable way to list all the files in a specified directory using only the C Standard library.

Functions like readdir are part of POSIX but not part of the C Standard library.

You have to remember that not all operating systems have a concept of directory. An example is the MVS OS on IBM System/360 and System/370 where "." was used in file names to represents a directory hierarchy."



POSIX is not something you add, it is a standard which most OS's support (but not windows - hence the header file I linked to). So #including dirent.h (as you would stdio.h for fopen) would allow you to enumerate a directory with the code below.


DIR *dir;
struct dirent *ent;

if ((dir = opendir ("c:\\src\\")) != NULL) {
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL) {
printf ("%s\n", ent->d_name);
}
closedir (dir);
} else {
/* could not open directory */
perror ("");
return EXIT_FAILURE;
}

Which is pretty simple? I implemented all of our filesystem handling code at work using that and it works on Windows/Linux/Mac/Android/iOS.

Also, if you are using C++17 - then that has a cross platform Filesystem library as part of the standard. https://en.cppreference.com/w/cpp/experimental/fs

Which would allow you to do this:

#include <string>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;

int main()
{
std::string path = "path_to_directory";
for (auto & p : fs::directory_iterator(path))
std::cout << p << std::endl;
}

But I wouldn't rely on that becoming generally available/stable for a while.