How to reading multiple text file

Hi all

Here is what I want to do:

There are around 200 text files of test results in a folder that I want to read them one by one and extract data that I want.

These file have standard name structure, e.g id + date.

I have searched on this forum, but what they want to do is rather simple, and can be done by storing an array of name manually in code. However, I don't think it is the best way for my application.

I have idea of how the steps should be, but I have no idea how to implement it with C++, so here is what I think I should do

1. Have a function to get a list of the file in the folder
2. Store all these names in an array
3. Using a loop function, reading the text file by fstream with argument from these name array, one by one.
4. Ideally I may do a check before passing the argument into fstream, e.g. if the extension name, the last three characters are not 'txt', then I pass this name.
5. I do whatever I do with this text file as usual
6. When I finish with this file, go back into the loop and start the next one.

And now, what I am struggling, is the very first step, how to get a list of the file name in a folder.

Can anyone give me some ideas please?

Many many thanks.
And now, what I am struggling, is the very first step, how to get a list of the file name in a folder.


What operating system are you using, and how cross-platform do you want the answer to be?
if you know the general name of the files,you may try to get to use a double variabled for loop to set the names of the files in a character pointer string.....
@Moschops

Just on windows 7.

@coder777

How to use it please? because I am really a newbie.

@Frooster

The id of them can be random, and the date are not continuous, so is hard to use a formula to 'predict the name' and use them.
If you only want this for windows you could use the FindFirstFile / FindNextFile functions, there's an example to work with here:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa365200(v=vs.85).aspx
If you are using Visual Studio 2015 you can use the experimental filesystem header (I'm not sure if MinGW has it yet).
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <filesystem>

int main() {
	using namespace std::experimental::filesystem;
	path p = "c:/";
	for (const auto& f : directory_iterator(p))
	{
		std::cout << f << std::endl;
	}
}
Last edited on
@naraku9333

Many thanks, it works very well!!

Topic archived. No new replies allowed.