problem writing data to .txt file

I have data which has been processed and stored into a data structure, data_base, defined by 3 classes. The data has the following form with thousands of lines:

//energy, energy_loss, x_pos, y_pos, z_pos
6.452757 1.531386 -0.393354 1.103488 107.863468
5.078611 1.374147 -0.413115 1.116262 108.052493
3.794508 1.284103 -0.427070 1.128054 108.200915
2.464638 1.329870 -0.428052 1.142637 108.316191
0.960768 1.503870 -0.430519 1.152415 108.398472
0.000000 0.960768 -0.430475 1.154462 108.421579

6.625408 1.402283 0.322641 1.591752 104.882207
5.260457 1.364951 0.329267 1.607333 105.077547
3.976761 1.283696 0.333221 1.618724 105.231689
2.713429 1.263332 0.334553 1.625161 105.352229
1.264016 1.449413 0.330530 1.630337 105.441501
0.000000 1.264016 0.329969 1.632061 105.477167

6.212022 1.475655 -1.736159 1.360011 105.789436
4.901267 1.310755 -1.699868 1.369173 105.968425
3.686299 1.214968 -1.669443 1.380009 106.109465
2.358059 1.328240 -1.640704 1.383770 106.219239
0.922748 1.435311 -1.620864 1.386110 106.296610
0.000000 0.922748 -1.614663 1.386681 106.317501

.
.
.

This data is stored into a vector, dm_data::data_base which can be defined by the following 3 classes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class dm_Step
{
public:
	float m_energy;
        float m_energy_loss;
	float m_xpos;
	float m_ypos;
	float m_zpos;

	void print()
	{
		ofstream test_file("C:\\Documents\\test.txt");
		test_file << m_energy << " " << m_energy_loss << " " << m_xpos << " " << m_ypos << " " << m_zpos << endl;
	}
};


1
2
3
4
5
6
7
8
9
10
11
12
13
class dm_Track
{
public:
	vector<dm_Step> m_track;

	void print()
	{
		ofstream test_file("C:\\Documents\\test.txt");
		for(int c = 0; c < m_track.size(); c++)
			m_track.at(c).print();
	}

};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class dm_Data
{
public:
	vector<dm_Track> m_data;

	void print()
	{
		for(int c = 0; c < m_data.size(); c++) 
		{
			ofstream test_file("C:\\Documents\\test.txt");
			m_data.at(c).print();
			test_file << endl;
		}
	}

};


Using this data structure for example,
data_base.m_data.at(0).m_track.at(0).m_energy = 6.452757 which is the energy value as seen on the first line of the data.

As you can see I also included a print() function to output the data to a file, test.txt, when called. When I try to use this print() function, nothing is output to my text file:

1
2
3
4
5
6
7
8
9
10
11
void main
{
.
.
.

   data_base.print()
.
.
.
}


What am I doing wrong? Am I correctly using the function defined in my classes to output this data?
Last edited on
The problem is that in the member function 'print()' of the class 'dm_Track' you 'open' the file, therefore when you call the 'print()' of the class 'dm_Step' and open the file, it's already opened , then the function fail and set the failbit. Also when create or open a file, you MUST specify the mode (input,output,append,ecc.).For the failure you can retrieve this by calling the fail() function in this way:
1
2
3
4
5
6
ofstream file("myFile.txt",ios::ate);  // <--- create an ofstream with name AND the mode (ios::ate open the file and move the position at the end of the file for append
if ( file.fail()) cout<<"Error!";  //if the file isn't opened , print a message
else{
    // operation of I/O
    file.close();     //  <-------- also you MUST close the file if you want so save changes to file
}
I think it is because you are opening the same file over and over. I'm using a Mac and the file system may do something different. But either way it's not good. Here is an example.
Bad
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
// Bad!!!!

#include <iostream>
#include <fstream>
using namespace std;

void foo3(){
   ofstream outStrm("./out.txt");
   for (int i(0); i<2; i++){
      outStrm << "foo3(" << i << ")" << endl;
   }
}

void foo2(){
   ofstream outStrm("./out.txt");
   for (int i(0); i<2; i++){
      foo3();
      outStrm << "foo2(" << i << ")" << endl;
   }
}

void foo1(){
   ofstream outStrm("./out.txt");
   for (int i(0); i<2; i++){
       foo2();
       outStrm << "foo1(" << i << ")" << endl;
   }
}

int main(){
   foo1();
   return 0;
}
$ ./a.out
$ cat out.txt 
foo3(0)
foo1(1)
$

Good
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
// Good!
#include <iostream>
#include <fstream>
using namespace std;

void foo3(ofstream &outStrm){
   for (int i(0); i<2; i++){
      outStrm << "foo3(" << i << ")" << endl;
   }
}

void foo2(ofstream &outStrm){
   for (int i(0); i<2; i++){
      foo3(outStrm);
      outStrm << "foo2(" << i << ")" << endl;
   }
}

void foo1(ofstream &outStrm){
   for (int i(0); i<2; i++){
       foo2(outStrm);
       outStrm << "foo1(" << i << ")" << endl;
   }
}

int main(){
   ofstream outStrm("./out.txt");
   foo1(outStrm);
   outStrm.close();
   return 0;
}
$ ./a.out
$ cat out.txt 
foo3(0)
foo3(1)
foo2(0)
foo3(0)
foo3(1)
foo2(1)
foo1(0)
foo3(0)
foo3(1)
foo2(0)
foo3(0)
foo3(1)
foo2(1)
foo1(1)

Pass the file all the way down.
@Vins3Xtreme: fstream's destructor will call close() if is_open() returns true. So just letting it go out of scope is enough to save and close the file ;)
ok so I modified my classes to the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class dm_Step
{
public:
	float m_energy;
	float m_energy_loss;
	float m_xpos;
	float m_ypos;
	float m_zpos;

	
	void print(ofstream &test_file)
	{
		test_file << m_energy << " " << m_energy_loss << " " << m_xpos << " " << m_ypos << " " << m_zpos << endl;
	}
};


1
2
3
4
5
6
7
8
9
10
11
12
class dm_Track
{
public:
	vector<dm_Step> m_track;

	void print(ofstream &test_file)
	{
		for(int c = 0; c < m_track.size(); c++)
			m_track.at(c).print();
	}
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class dm_Data
{
public:
	vector<dm_Track> m_data;

	
	void print(ofstream &test_file)
	{
		for(int c = 0; c < m_data.size(); c++) 
		{
			m_data.at(c).print();
			test_file << endl;
		}
	}

};


And using the print function in my main code:

1
2
3
ofstream test_file("C:\\test.txt");
	data_base_1.print(test_file);
	test_file.close();


When I try to compile this code I get the following error:

Error 2 error C2660: 'dm_Step::print' : function does not take 0 arguments
Error 4 error C2660: 'dm_Track::print' : function does not take 0 arguments

What is going on???
Last edited on
when you call the 'print()' funztion of 'dm:Step' you must insert the parameter of type ostream:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class dm_Data
{
public:
	vector<dm_Track> m_data;

	
	void print(ofstream &test_file)
	{
		for(int c = 0; c < m_data.size(); c++) 
		{
			m_data.at(c).print(test_file); // <---- added the parameter
			test_file << endl;
		}
	}

};

and here
1
2
3
4
5
6
7
8
9
10
11
class dm_Track
{
public:
	vector<dm_Step> m_track;

	void print(ofstream &test_file)
	{
		for(int c = 0; c < m_track.size(); c++)
			m_track.at(c).print(test_file);  // <--- added the parameter
	}
};

Topic archived. No new replies allowed.