using an fstream in a class

I'm in a college c++ course and we have started writing our own classes.

I was trying to figure out if its possible to create an ofstream in the private section so that it could be used by the entire class. If thats not possible, whats the easiest way to use ifstream and ofstream so the entire class can read and write from the same file?

an example of an ofstream Im using would be like this:
1
2
ofstream fout;
fout.open("C:/Users/Josh/Desktop/sequences.dat");


And just tried throwing that into the private section but of course it doesnt work lol
It does work, make sure to post your code.
Unless you literally put fout.open... below the declaration. Then you need to move it to the constructor.
Yeah I literally put it under the declaration, if I have two constructors do I put it in both?

Here is the code for my header:

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

#ifndef ROVER_H
#define ROVER_H 

#include <vector>
#include <fstream>


class rover
{
public:
	// CONSTRUCTORS
	rover();
	//Default constructor
	// POSTCONDITION: The rover is initialized with default coordinates (0,0) heading 
	// North
	
	rover(double initial_x, double initial_y, char intitialHeading);
	//Default constructor
	// POSTCONDITION: The rover is initialized with coordinates heading specified
	// by the user
	
	// MODIFICATION MEMBER FUNCTIONS
	void move_forward(double meters);
	//Function to move the rover forward a max of 25 meters
	// POSTCONDITION: The rover has moved in the direction of its heading
	// by the specified number of meters
	
	void move_backward(double meters);
	//Function to move the rover in reverse a max of 4 meters
	// POSTCONDITION: The rover has moved in the opposite direction of its heading
	// by the specified number of meters

	void turn_right();
	//Function that turns the rover 90 degrees to the right
	// POSTCONDITION: The rover's heading has moved 1 direction clockwise

	void drill_core();
	//Function that drills a core at current location and stores the location
	//POSTCONDITION: A core has been drilled and its location has been stored


	// CONSTANT MEMBER FUNCTIONS
	void current_location() const;
	//Function to report the rovers location and heading
	// POSTCONDITION: The coordinates and heading are returned as an ordered
	// triple

	int core_quantity() const;
	//Function that reports the number of cores drilled
	//POSTCONDITION: the quantity of core samples has been outputed
	
	void core_report() const;
	//Function that reports quantity of cores drilled as well as their locations
	//POSTCONDITION:  All cores and their locations have been outputed
	


private:
	double first_x;
	double first_y;
	double x;
	double y;
	char heading;
	std::vector<double> core_xVec;
	std::vector<double> core_yVec;
	std::vector<double> distanceVec;
	int quantity;
	
        ofstream fout;
        fout.open("C:/Users/Josh/Desktop/sequences.dat");   

        //where would this fout.open go?
        // I can post my implementation file as well if needed

};

#endif 
Yes put it in both constructors.
You can also put it in an initialize() method and call that one in both constructors.
That way, you can avoid duplicate code.
Athar makes a good point. Even better though just put it in rover(double initial_x, double initial_y, char intitialHeading) and then call that one inside rover(void). (I think you have to use the keyword this.) (If you don't understand what I just said don't bother, just do what Athar said.)
Yeah I got a little lost following you Kevin lol What about closing the file? I can seem to figure out where I could close the file if I am outputting to my file from multiple functions
I start closing the file by creating a member function that closes fout, and calling that function when I need to.

But now I have a new porblem, I learned that I need to have more than one "rover" at once, which means that instead of having one file, the class needs to be able to create a file for each rover everytime one is created. For instance:

1
2
rover mars(1, 2, E);
rover pluto;


each need to have their own files that they can send their movement information too...I'm pretty lost on how to do this because there is not a set number of files because it needs to be able to create as many files as the user creates new rovers.

I'm also not sure how I could tell the member functions which file to write to when a rover moves. If I say mars.move_forward(5), how can I let the move_forward function know to write to the mars file instead of the pluto file?
Topic archived. No new replies allowed.