print to file from class

My code prints out in the console. I want to write the data to a text file. I know how to do this the Main program but this is in my class .cpp file. How can I modify something like ofstream myfile for the class file? Is it bad practice to open file like I did below?


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
28
29
30
31
32
33
34
35
36
37
// Print Rectangle coordinates
ostream & poly1::operator<<(ostream &os, Rectangle &PointRectangle)
{
	Matrix Temp1(2,1);
	Matrix Temp2(2,1);
	Matrix Temp3(2,1);
	Matrix Temp4(2,1);
	
	Temp1=PointRectangle.GetV1();
	Temp2=PointRectangle.GetV2();	
	Temp3=PointRectangle.GetV3();	
	Temp4=PointRectangle.GetV4();

	os<<"__________________________________________________________________________"<<endl; 
	os<<"Vertex\t1\t\t2\t\t3\t\t4"<<endl; 
	os<<"__________________________________________________________________________"<<endl; 
	os<<"\t"<<Temp1.GetMatrixData(1,1)<<"\t\t"<<Temp2.GetMatrixData(1,1)<<"\t\t"<<Temp3.GetMatrixData(1,1)<<"\t\t"<<Temp4.GetMatrixData(1,1)<<endl; 
	os<<"\t"<<Temp1.GetMatrixData(2,1)<<"\t\t"<<Temp2.GetMatrixData(2,1)<<"\t\t"<<Temp3.GetMatrixData(2,1)<<"\t\t"<<Temp4.GetMatrixData(2,1)<<endl; 
	os<<"__________________________________________________________________________"<<endl;  

	//==============================================
	ofstream myfile;
	myfile.open ("ShapeData.txt");
	myfile << "Writing this to a file.\n";

	myfile<<"__________________________________________________________________________"<<endl; 
	myfile<<"Vertex\t1\t\t2\t\t3"<<endl; 
	myfile<<"__________________________________________________________________________"<<endl; 
	myfile<<"\t"<<Temp1.GetMatrixData(1,1)<<"\t\t"<<Temp2.GetMatrixData(1,1)<<"\t\t"<<Temp3.GetMatrixData(1,1)<<endl; 		
	myfile<<"\t"<<Temp1.GetMatrixData(2,1)<<"\t\t"<<Temp2.GetMatrixData(2,1)<<"\t\t"<<Temp3.GetMatrixData(2,1)<<endl;
	myfile<<"__________________________________________________________________________"<<endl;
	myfile.close();

	//==============================================

	return os;
}
Last edited on
If you put everything into a stringstream first then you can just write that to both the ostream and file using myFile << mystringstream.str();
I've now overloaded my opeartor to std::ostream. How do I now put my output into os.str() to output to myFile << os.str()? Thanks for the help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Print Triangle coordinates
std::ostream & poly1::operator<<(std::ostream &os, Triangle &PointTriangle)
{
	Matrix Temp1(2,1);
	Matrix Temp2(2,1);
	Matrix Temp3(2,1);

	Temp1 = PointTriangle.GetV1();
	Temp2 = PointTriangle.GetV2();
	Temp3 = PointTriangle.GetV3();

	os<<"__________________________________________________________________________"<<endl; 
	os<<"Vertex\t1\t\t2\t\t3"<<endl; 
	os<<"__________________________________________________________________________"<<endl; 
	os<<"\t"<<Temp1.GetMatrixData(1,1)<<"\t\t"<<Temp2.GetMatrixData(1,1)<<"\t\t"<<Temp3.GetMatrixData(1,1)<<endl; 		
	os<<"\t"<<Temp1.GetMatrixData(2,1)<<"\t\t"<<Temp2.GetMatrixData(2,1)<<"\t\t"<<Temp3.GetMatrixData(2,1)<<endl;
	os<<"__________________________________________________________________________"<<endl; 

	ofstream myfile;
	myfile.open ("ShapeData.txt");
	myfile << os.str();

	return os;
}
Last edited on
Topic archived. No new replies allowed.