Please support our C++ advertiser: Programming Forums
![]() |
•
•
Join Date: Oct 2007
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
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:
That are parts of my data mining project. Every help and advise would be much appreciated. Thanks.
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:
cpp Syntax (Toggle Plain Text)
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(); }
Last edited by WaltP : Oct 18th, 2007 at 6:26 am. Reason: Added CODE tags -- you actually typed right over how to use them when you entered this post...
#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.
#3) Read this about the following line (.eof() is the same as feof())
#4) Also read this to find out how to format code so it can be understood.
#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.
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
-- Pearl Williams
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,862
Reputation:
Rep Power: 40
Solved Threads: 1013
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;
}•
•
Join Date: Oct 2007
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 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.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,884
Reputation:
Rep Power: 13
Solved Threads: 197
The open() function only takes char * as an argument --it won't take std::string. Say this instead:
Hope this helps.
infile.open( filename.c_str(), ios::in );
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,862
Reputation:
Rep Power: 40
Solved Threads: 1013
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.
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();
•
•
Join Date: Oct 2007
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
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:
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:
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 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.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,862
Reputation:
Rep Power: 40
Solved Threads: 1013
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.
>> freq[filecounter]lineineachfile[filecounter]
I think you are missing square brackets in that statement.
Last edited by Ancient Dragon : Oct 19th, 2007 at 12:19 am.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Reading Binary Files in VB (Visual Basic 4 / 5 / 6)
- Multiple files (C)
- parsing multiple files in current directory (Perl)
- problems writing multiple files (Perl)
- problems with reading video files (Windows NT / 2000 / XP / 2003)
- multiple file searching (C)
- Use Java to remove a block of html from a number of files? (Java)
- loop to create arrays when reading a file (Java)
- Reading Keystates (C)
- How to Rename Multiple Files with Windows Explorer (Windows tips 'n' tweaks)
Other Threads in the C++ Forum
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode