Reading all files in a directory

Hi, Im trying to go through a folder and read all files inside the folder into a string made inside a class. Reading in one specific file is not problems, but I cant seem to find a good solution for reading in all the files(one at the time ofc.) Ive read through two books now and I cant find any solution for this, Ive searched on the web aswell, and I dont find a solution that uses c++ classes(Those Ive found are C ones, and I dont know how they would work with rest of my code).

I also tried _findfirst but its stated that _findfirst only returns the name, it ignores the directory, so I guess that wont work well(Unless Im using it wrong, which I probably am :P)

So, how would I load all files, one by one?

1
2
3
4
5
6
7
8
9
	string str_buffer;

	ifstream raidfile;
	raidfile.open ("logs/*.rl");	// DONT WORK
	raidfile >> str_buffer;
	raidfile.close();


	RaidString str(str_buffer);
Last edited on
I hope this chunk of code will help you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<stdio.h>
#include<cstdlib>
#include<iostream>
#include<string.h>
#include<fstream>
#include<dirent.h>

void listFile();

int main(){
    listFile();
    return 0;
}

void listFile(){
        DIR *pDIR;
        struct dirent *entry;
        if( pDIR=opendir("./data/item") ){
                while(entry = readdir(pDIR)){
                        if( strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0 )
                        cout << entry->d_name << "\n";
                }
                closedir(pDIR);
        }
}
Last edited on
fatal error C1083: Cannot open include file: 'dirent.h': No such file or directory

:S

Is it cause Im using Visual Studio?


EDIT: Nvm, found the file on the net :)
Last edited on
Hmmm, I managed to get your code running, but it doesnt really help :S

How do I get what entry->d_name returns into a stream?

Ive tried to use entry->d_name as a parameter, but it wont work :(
(Crashes when it tries to start the stream)
Following error message:

Windows has triggered a breakpoint in directory_reading.exe.

This may be due to a corruption of the heap, which indicates a bug in directory_reading.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while directory_reading.exe has focus.

The output window may have more diagnostic information.



here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<stdio.h>
#include<cstdlib>
#include<iostream>
#include<string.h>
#include<fstream>
#include<sstream>
#include<dirent.h>
using namespace std;
void listFile();

int main(){
    listFile();
    return 0;
}
void listFile(){
	ifstream inn;
	string   str;
	DIR *pDIR;
	struct dirent *entry;
    if( pDIR=opendir("./1") ){
		while(entry = readdir(pDIR)){
			if( strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0 ){
				inn.open(entry->d_name);
				inn >> str;
				cout << str;
			}
			closedir(pDIR);
		}
	}
}



Is there really no standard function in c++ to do this? :S

dirent.h is old isnt it?
I also read some .net way of doing it, but Id prefer to understand the way its thought to be done with ifstream before learning MS/windows ways :S:S
No, there's no standard way to do it.
No, it's not old. It's a POSIX header. So in a way, it is old, but my point is that it's not (entirely) one of those things to avoid.
You could do it with .NET, just as well as you could do it in BASIC or Python, but then that wouldn't be C++ anymore, now would it?

The problem seems to be that line 27 should be after the while loop, not inside it. The way it is now, pDIR is closed before the end of the first loop. Calling readdir() on a closed DIR obviously causes a segmentation fault.
Last edited on
Topic archived. No new replies allowed.