passing a file in a function

passing a file in a function

Page 1 of 1

3 Replies - 1909 Views - Last Post: 11 February 2008 - 09:19 AM Rate Topic: -----

#1 prads   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 22-October 07

passing a file in a function

Post icon  Posted 11 February 2008 - 03:59 AM

Hi,
In the following code I am trying to multiply the contents of two files and storing the value in a 3rd file for which i am calling a function and passing files through it. However I am not able to debug the function calling and/or passing of the files. Can anyone please help me.


#include <iostream>
#include <fstream>
#include <string>

#define TXT 20

using namespace std;
/////////////////////////////////////////////////////////////////
//mul(ifstream &input1,infstream &input2, ofstream &output)
//int mul(char file1[],char file2[],char fileout[])
int mul(ifstream & file1,ifstream & file2,ofstream & fileout)
{//char file1,file2,fileout;
	int  multiplicand[20][20],multiplier[20][20], product[20][20];
	int m1,n1,m2,n2,i,j,k;

	cout << "Enter the rows of Mltiplicand Matrix  "<< endl;
	cin>>m1;
	
	
	cout << "Enter the columnss of Mltiplicand Matrix "<<endl;
	cin>>n1;
	
	cout << "Enter the rows of Mltiplier Matrix  "<< endl;
	cin>>m2;
	
	cout << "Enter the columns of Mltiplicand Matrix  "<<endl;
	cin>>n2;


	 //try to open input/output
	ifstream input1(file1);
	if(input1.fail()){
	   cout<<"Error: could not find file."<<endl;
	   system("Pause");
	   exit(1);//abort program
	 	}

	  ifstream input2(file2);
	  if(input2.fail()){
	   cout<<"Error: could not find file."<<endl;
	   system("Pause");
	   exit(1);//abort program
	   }
	   
	ofstream output(fileout,ofstream::out);
	   if(output.fail()){
	   cout<<"Error: could not find file."<<endl;
	   system("Pause");
	   exit(1);//abort program
	 	}
   ///reading the multiplicand matrix	  
	for (i=0; i<m1;i++)
	{
		for(j=0; j<n1; j++)
		{
			input1>>multiplicand[i][j];
		}
	}
	///reading the multiplier matrix	  
	for (i=0; i<m2;i++)
	{
		for(j=0; j<n2; j++)
		{
			input2>>multiplier[i][j];
		}
	}
///Multiplication	
	for(i=0;i<m1;i++)
	{
		for(j=0; j<n2;j++)
		{
			
			
			product[i][j]=0;
			
			for(k=0;k<n1;k++)
			{
				
	   			
	   			//multipliation ..parameters sent to the function
				  product[i][j]+=multiplicand[i][k]*multiplier[k][j];
	}
	}}
	
	
	//printing the product in file and screen
	for(i=0;i<m1;i++)
	{
		for(j=0;j<n2;j++)
		{
				cout<<product[i][j]<<" ";
	   			output<<product[i][j]<<" ";
	   	}
	   	cout<<endl;
	   	output<<endl;
	 }
		
	//cout<<endl;
	//output<<endl;

return 0;
	
} 





/////////////////////////////////////////////////////////////
int main()
{
	
	char file1[TXT],file2[TXT],fileOut[TXT];
	////multiplicand matrix[m1xn1], multiplier matrix[m2xn2]
	

	//add(char,char,char);
	
	cout << "Enter the Multiplicand file name." << endl;
	cin >> file1;
	
	cout <<" Enter the Multiplier file name ."<<endl;
	cin >> file2;
	
	cout << "Enter the Product file" << endl;
	cin >> fileOut;
		
	
	// mul(input1,input2, output);
	
int mul(char file1[],char file2[],char fileout[]);//calling function to multiply
//	add(char file1,char fil2, char fileout);//calling function to add

system("Pause");
	return 0;
}//end of main

	
	
//function to multiply begins here	
	




Thanks,
prads

Is This A Good Question/Topic? 0
  • +

Replies To: passing a file in a function

#2 Trogdor   User is offline

  • D.I.C Addict
  • member icon

Reputation: 15
  • View blog
  • Posts: 627
  • Joined: 06-October 06

Re: passing a file in a function

Posted 11 February 2008 - 04:04 AM

After 69 posts you should know the forumrules.
What is it doing, what not, what do you expect, what errors do you get, etc, etc.
Was This Post Helpful? 0
  • +
  • -

#3 prads   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 22-October 07

Re: passing a file in a function

Posted 11 February 2008 - 04:32 AM

Oops!!!!! sorry.......
anyways here it is.......
It executes the main() function, is not performing the function call "int mul(char file1[],char file2[],char fileout[]);".
I suspect an error in the calling and called function declaration i.e. esp the file passing parameters.

Also its giving the following error:
"Error 1 error C2664: 'std::basic_ofstream<_Elem,_Traits>::basic_ofstream(const char *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'std::ofstream' to 'const char *' "

Thanks,
prads
Was This Post Helpful? 0
  • +
  • -

#4 Trogdor   User is offline

  • D.I.C Addict
  • member icon

Reputation: 15
  • View blog
  • Posts: 627
  • Joined: 06-October 06

Re: passing a file in a function

Posted 11 February 2008 - 09:19 AM

that should give you a hint to the problem.
i can see you pass file1 flie2 etc to mul. they are char * (or char[], the same)
but the function you declared expects parameters of another type.
above the declaration you comented out another version:
//int mul(char file1[],char file2[],char fileout[])

that one accepts the same parametertypes you are passing, so should at least compile for so far.
In mul things might go wrong with filehandling, but the immediate error you are getting now is simply a type checking problem.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1