RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 3271 | Replies: 9 | Thread Tools  Display Modes
Reply
Join Date: Oct 2007
Posts: 4
Reputation: cloudet is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
cloudet cloudet is offline Offline
Newbie Poster

Question reading multiple files in loop

  #1  
Oct 18th, 2007
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:

  1. ifstream infile;
  2. int indexx;
  3. string st1 = "file";
  4. string st2 = ".txt";
  5. for (int i=0; i<filesize; i++)
  6. {
  7. infile.open(strcat(strcat(st1,(strcat((string)i,st2))),ios::in)
  8. while (!infile.eof())
  9. {
  10. infile>>words[i][indexx]>>numbers[i][indexx];
  11. indexx++;
  12. }
  13. infile.close();
  14. }
That are parts of my data mining project. Every help and advise would be much appreciated. Thanks.

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...
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2006
Posts: 2,814
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 16
Solved Threads: 240
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: reading multiple files in loop

  #2  
Oct 18th, 2007
#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.
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,862
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 1013
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: reading multiple files in loop

  #3  
Oct 18th, 2007
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;
}
<<Hire Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Oct 2007
Posts: 4
Reputation: cloudet is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
cloudet cloudet is offline Offline
Newbie Poster

Re: reading multiple files in loop

  #4  
Oct 18th, 2007
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.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,884
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 197
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: reading multiple files in loop

  #5  
Oct 18th, 2007
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.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,862
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 1013
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: reading multiple files in loop

  #6  
Oct 18th, 2007
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();
<<Hire Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Oct 2007
Posts: 4
Reputation: cloudet is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
cloudet cloudet is offline Offline
Newbie Poster

Re: reading multiple files in loop

  #7  
Oct 18th, 2007
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.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,862
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 1013
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: reading multiple files in loop

  #8  
Oct 19th, 2007
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.
Last edited by Ancient Dragon : Oct 19th, 2007 at 12:19 am.
<<Hire Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Oct 2007
Posts: 4
Reputation: cloudet is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
cloudet cloudet is offline Offline
Newbie Poster

Re: reading multiple files in loop

  #9  
Oct 19th, 2007
Ancient Dragon,
I tried stringstream but failed. Thus i tried searching internet and found sprintf, and it works well without error.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,862
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 1013
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: reading multiple files in loop

  #10  
Oct 19th, 2007
you had it all right except the open statement, and Duoas gave you the correct way to do that.
<<Hire Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:39 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC