Read multiple files(.txt) from a folder

Hello guys,

I have a code that read txt file. But now I Need a code For reading multiple files(.txt) from a folder and save each one in separate array.

Any help would appreciate
Hi abotaha, can you post your code for reading a txt file, from there we can maybe guide you on how to adapt it for more than 1 file.
you can use the boost library filesystem for that purpose http://www.boost.org/doc/libs/1_44_0/libs/filesystem/v2/doc/index.htm
Last edited on
yes Moooce. The code is below but it is noly for reading one txt file. I do not know how i make the code read many text files from one folder.



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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

int main ()
{
        string STRING;
	ifstream infile;
	float sat[6];
	int j, i,k=0;
	infile.open ("y.txt"); 
    i=0;
    while(!infile.eof())
        {
	        getline(infile,STRING); 
	        stringstream ss(STRING);
	        while(ss >> sat[i])
                i++;k++; 
        }
	infile.close();
	for(j=0;j<k;j++) cout<<sat[j]<<"\n";
	system ("pause");
	return 0;
}
Last edited on
OK, I've tried running your code, and it only reads 1 line of text into a string, so a problem there...

I've not used stringstreams before so I'm not sure how they work but it looks like you have all sorts of variables doing things for no reason in the code.

Try using the code below in your main function instead to read a text file and see if it works.
1
2
3
4
5
6
7
ifstream infile;
string tmpstring,mystring;
	infile.open ("y.txt"); 
	while(getline(infile,tmpstring))
		mystring+=tmpstring;

	infile.close();


Then print out what has been read from the file with :
cout << mystring << endl;

If you can get that working to read 1 file, and understand what it's doing, the next thing would be to make mystring into an array of strings and place the text reading code into a function that you can call, passing in different filenames and an element of your string array.

See how you do, I can prompt you if you have trouble :-)
Last edited on
Please don't do this:
1
2
3
4
5
while(!infile.eof())
{
	getline(infile,STRING); 
	// use STRING
}

EOF will not be flagged until *after* the read fails so you will process one line of bad data.
Use this:
1
2
3
4
while(getline(infile,STRING))
{ 
	// use STRING
}
Last edited on
You're right Galik, but the copy+paste gremlin caught me out.
Thanks guys for the comments. There is another idea by renaming the text files as data_0.txt, data_1.txt, and so no.
The idea is to read each file by changing the file name and that is by concatenate the part of file name which is data_ with the integers 0,1,2...etc. within loop.
the code is :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <sstream>
#include <iostream>
#include <string>

using namespace std;
int main()
{
	string filename;
	ostringstream ss;
	filename="data_";
	for(int i;i<2;i++)
	{
	ss << filename << i;
	FILE * f = fopen (ss.str(), "r");
			 if (NULL != f)
			 {
				 ///.....some work
			 }
			 else
			 printf ("Could not open the file\n");
	}
	return 0;
}

but i got this error:
cannot convert `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to `const char*' for argument `1' to `FILE* fopen(const char*, const char*)'

Help please
This:
 
FILE * f = fopen (ss.str(), "r");

would need to be like this:
 
FILE * f = fopen (ss.str().c_str(), "r");

But this would be better:
1
2
3
std::ifstream infile;

infile.open (ss.str().c_str());

Thank you Galik, it works now but the funny thing which i cannot understand, is the code cannot open and read the text file. The output is:Could not open the file


the code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <sstream>
#include <iostream>
#include <string>

using namespace std;
int main()
{
	string filename;
	ostringstream ss;
	filename="Data_";
	for(int i=0;i<2;i++)
	{
	ss << filename << i;
	FILE * f = fopen (ss.str().c_str(), "r");
			 if (NULL != f)
			 {
				 printf("Can open the file\n");
			 }
			 else
			 printf ("Could not open the file\n");
	}
	system("pause");
	return 0;
}


and as an example for the Data text file is
1
2
3
4
5
6
7
8
9
3.31
3.31
2.75
2.74
3.36
3.31
2.96
2.71
3.36
All right....The code works well now with eclipse environment but it is not work with Dev-C++.
anyhow it works now..

Thanks for all of you.
Topic archived. No new replies allowed.