How do I open a directory in c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How do I open a directory in c++

I've been reading about the <fstream> lately, I read a lot about but I never even a word talking about directories, I read all about files. now I want to open a directory but I just can't.

29th Jun 2017, 7:26 PM
Luyanda
Luyanda - avatar
9 Answers
+ 5
@Luyanda A pure windows version 😐... https://code.sololearn.com/cHuxY17co2X2/?ref=app This may help you. It gets the list of readable folders, their size and number of files inside them, in a path entered by a user. Eg Entering C:\\ returns the data of the folders present in C drive of the computer (Counts only the folders)
1st Jul 2017, 1:29 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 4
DIR *d; d = opendir(path);
29th Jun 2017, 7:57 PM
shaldem
shaldem - avatar
+ 4
You can use function opendir to open directory. On the 'path' place of my example, you need to use pointer to a character array, with the path to directory you need to open. You need include dirent.h lib for using this function.
30th Jun 2017, 10:29 AM
shaldem
shaldem - avatar
+ 4
@Luyanda No, Im afraid not. I will work on a linux version, but just can't get the time. I have so much to learn in linux... Windows.h is not at all cross- platform, and I can't see ncurses in CppDroid...
7th Jul 2017, 2:22 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
What do you mean by opening a directory? Do you want to open a file in a specific directory?
29th Jun 2017, 7:37 PM
Lukas Klose
Lukas Klose - avatar
+ 2
@shaldem. thanks a lot. let me try now.
30th Jun 2017, 10:33 AM
Luyanda
Luyanda - avatar
+ 2
@kishuk thanks will this code run on CppDroid?
1st Jul 2017, 4:03 PM
Luyanda
Luyanda - avatar
+ 1
@shaldem can you plz explain more
30th Jun 2017, 10:17 AM
Luyanda
Luyanda - avatar
+ 1
@Lukas klose. I want to open my storage disc and see the folder names using CPP.
30th Jun 2017, 10:19 AM
Luyanda
Luyanda - avatar