Dear all gurus,
Given 30 files, with same format (2 columns, column1 is string and column2 is int. contains thousands of lines) and uniform file name, eg: file1.txt, file2.txt,...,file30.txt,
How can i read it in a loop, and then load them into my 2D array (eg: string words [filesize][maxline], and int numbers [filesize][maxline])?

Tried to use strcat and append but cant work coz the integer i:

ifstream infile;
int indexx;
string st1 = "file";
string st2 = ".txt";
for (int i=0; i<filesize; i++)
{
infile.open(strcat(strcat(st1,(strcat((string)i,st2))),ios::in)
while (!infile.eof())
{
infile>>words[i][indexx]>>numbers[i][indexx];
indexx++;
}
infile.close();
}

That are parts of my data mining project. Every help and advise would be much appreciated. Thanks.

:)

#1) You can't use strcat on strings. Only with char*. Look up string methods.

#2) Break the following line into pieces to create the file name and output it so you know it's correct before using the open function. It's also less confusing. infile.open(strcat(strcat(st1,(strcat((string)i,st2))),ios::in) #3) Read this about the following line (.eof() is the same as feof()) while (!infile.eof()) #4) Also read this to find out how to format code so it can be understood.

Here is an example of one way to construct those filenames

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
    string st1 = "file";
    string ext = ".txt";
    string filename;
    for(int i = 1; i < 10; i++)
    {
        stringstream ss;
        ss << i;
        filename = st1 + ss.str() + ext;
        cout << filename << "\n";
        
    }
    return 0;
}
void readfileinarray (ifstream &infile, int lcount,int indexx)
{
	string st1 = "file";
    string ext = ".txt";
    string filename;
	for (int i=0; i<filesize; i++)
	{
		stringstream ss;
		ss<<i;
		filename = st1 + ss.str() +ext;
	infile.open(filename,ios::in)
		if(!infile){
    cerr << "Cannot open file"<<i<<".txt"<<endl;
	system("pause");
	}
	else{
	while (!infile.eof())
	{
		infile>>terms[i][indexx]>>freq[i][indexx];
		indexx++;
	}
	infile.close();
		}
	}
}

Hi, Ancient Dragon
I tried to use stringstream, but still there is error:

error C2664: 'void std::basic_ifstream<_Elem,_Traits>::open(const wchar_t *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'std::string' to 'const wchar_t *'

Any idea wat's the error is?
Or other suggestion?
Thanks in advance.
:)

The open() function only takes char * as an argument --it won't take std::string. Say this instead:

infile.open( filename.c_str(), ios::in );

Hope this helps.

since infile is of type ifstream it is not necessary to specify ios::in because that is the default

after closing the stream you have to also clear the error bits because if you don't the file will not open the second and succeeding loop iterations.

infile.close();
infile.clear();

Thanks for all the helps, everyone.
I tried sprintf and get it done. I can now read multiple files into array. To share to u all, here's the codes:

char st1 [10]= "file";
    char ext [10]= ".txt";
	char filename [30] = {'\0'};
	
	for (filecounter=1; filecounter<4; filecounter++)
	{
		infile.seekg(0,ios::beg);
		infile.clear(0);
		sprintf_s(filename, "%s%d%s", st1, filecounter, ext);
		infile.open(filename);
			if(!infile){
			 cerr << "Cannot open"st1<<filecounter<<ext<<endl;
			system("pause");
			}
	else{
	while (!infile.eof())
	{
		infile>>terms[filecounter][indexx]>>freq[filecounter][indexx];
		//cout<<terms[filecounter][indexx]>>freq[filecounter][indexx];
		indexx++;
	}
	lineineachfile[filecounter]=indexx;

	infile.close();
	infile.clear();
		}//else
	}

Now i faced one more problem:
How if i wan to cout or manipulate each file. with the exact maxnumber of lines?
This is wat i've done but it doesn't seems right:

int i= 1;
	while(i != filecounter){
		for (int j=1;j<filecounter; j++){
			for (int k =0; k<lineineachfile[filecounter];k++)
				cout<<terms[filecounter][lineineachfile[filecounter]]<<" " <<freq[filecounter]lineineachfile[filecounter]<<endl;
		}
		filecounter++;
	}

filecounter is to counter number of file,
lineineachfile counts the maxline of a file, and it is an array : int lineineachfile [].
Please advise.

Thanks in advance.
:)

I thought you are supposed to be writing a c++ program? If so, then why are you resorting to C language syntax and C functions?


>> freq[filecounter]lineineachfile[filecounter]

I think you are missing square brackets in that statement.

Ancient Dragon,
I tried stringstream but failed. Thus i tried searching internet and found sprintf, and it works well without error.
:)

This article has been dead for over six months. Start a new discussion instead.