Problem with writing/reading to file

Hello,

I am having some trouble with reading and writing to files. I create an array of pointers to the object Distance. I write these to a file with the statement:
dist_file.write((char*)(ptrd[i]), sizeof(Distance))
and read them back in using the statement:
dist_file.read( (char*)ptr_df[n], sizeof(Distance) );

The output does not really resemble the notation of feet and inches:

Read 8 distances.
8204304'-0"
8204416'-0"
8204448'-0"
17567760'-0"
17567872'-0"
17567904'-0"
6311952'-0"
0'-0"



What am i doing wrong? Complete code below.

Cheers,
Hans



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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
#include <fstream>

using namespace std;

class Distance
    {
	private:
	    int feet;
	    float inches;
	public:
	    Distance() : feet(0), inches(0.0)
		{}
	    Distance(int ft, float in) : feet(ft), inches(in)
		{}
	    ~Distance()
		{}
	    
	    void getdist()
		{
		    cout << "\nEnter feet: ";
		    cin >> feet;
		    cout << "Enter inches: ";
		    cin >> inches;
		}
	    
	    void showdist()
		{
		    cout << feet << "\'-" << inches << "\"";
		}

	    void add_dist(Distance, Distance);
    };

void Distance::add_dist(Distance d2, Distance d3)
    {
	inches = d2.inches + d3.inches;
	feet = 0;
	if ( inches > 12.0 ) {
	    inches -= 12.0;
	    feet++;
	}
	feet += d2.feet + d3.feet;
    }

int main()
    {
	const int SZ = 100;
	Distance* ptrd[SZ];
	Distance* ptr_df[SZ];
	int dist_obj = 0;
	char answer = 'e';
	int n = 0;
	fstream dist_file;

	cout << "Distance Program v0.0.1";
	cout << "\nEnter a to add distances.";
	cout << "\nEnter d to display file.";
	cout << "\nEnter h for help.";
	cout << "\nEnter p to print distances.";
	cout << "\nEnter s to save distances.";
	cout << "\nEnter q to quit." << endl;

	do {
	    cout << "\nEnter command (a,d,h,p,s,q): ";
	    cin.unsetf(ios::skipws);
	    cin >> answer;
	    cin.setf(ios::skipws);
	    
	    if ( cin.good() ) {
		cin.ignore(10, '\n');
		switch (answer) {
		    case 'a':
			    while ( answer != 'n' ) {
				cout << "\nEnter a new distance(y/n)?";
				cin.unsetf(ios::skipws);
				cin >> answer;
				cin.setf(ios::skipws);
				if ( cin.good() && answer == 'y' ) {
				    ptrd[dist_obj] =  new Distance;
				    ptrd[dist_obj++]->getdist();
				    cin.ignore(10, '\n');
				} else if ( cin.good() && answer == 'n' ) {
				    break;
				} else {
				    cin.clear();
				    cout << "\nIncorrect input: enter y or n." << endl;
				}
			    }
			    cin.ignore(10, '\n');
			    break;
		    case 'd':
			    dist_file.open("dist_file.dst", ios::in | ios::binary);
			    if ( !dist_file ) {
				cout << "\nCan not open file dist_file.dst.";
				break;
			    }
			    dist_file.seekg(0);
			    n = 0;
			    while ( !dist_file.eof() ) {
				ptr_df[n] = new Distance;
				dist_file.read( (char*)ptr_df[n], sizeof(Distance) );  /*Is this correct?*/
                                n++;          //create some file...
			    }
                            dist_file.close();
			    cout << "\nRead " << n << " distances." << endl;
			    for ( int i = 0; i < n; i++ ) {
				ptr_df[i]->showdist();                                               /*output produces garbage*/
				cout << endl;
			    }
			    break;
		    case 'h':
			    cout << "\nEnter a to add distances.";
			    cout << "\nEnter d to display file.";
			    cout << "\nEnter h for help.";
			    cout << "\nEnter p to print distances.";
			    cout << "\nEnter s to save distances.";
			    cout << "\nEnter q to quit." << endl;
			    break;
		    case 'p':
			    if ( dist_obj == 0 ) {
				cout << "\nNo distances added, try option a or d.";
			    } else {
				for(int i=0; i < dist_obj; i++) {
				    ptrd[i]->showdist();
				    cout << endl;
				}
			    }
			    break;
		    case 's':
			    if ( dist_obj == 0 ) {
				cout << "\nNothing to save. Exiting command.";
			    } else {
				dist_file.open("dist_file.dst", ios::app | ios::out | ios::binary);
				if ( !dist_file ) {
				    cout << "\nCan not open file to write.";
				    break;
				}
				for (int i=0; i < dist_obj; i++)
				    dist_file.write((char*)(ptrd[i]), sizeof(Distance));
				dist_file.close();
				cout << "\nFile saved.";
			    }
			    break;
		    case 'q':
			    break;
		    default:
			    cout << "\nEnter a, d, p, s or q please.";
			    break;
		}
	    } else {
		cin.clear();
		cout << "\nIncorrect input: enter a, d, p, s or q please." << endl;	
	    }
	} while ( answer != 'q');
 
	return 0;
    }
Little update:

when changing line 140 to:

dist_file.write(reinterpret_cast<char*>(ptrd[i]), sizeof(Distance));

it seems to work. Is this THE way to write objects to a file or are there others when using binary mode?
High

Nope. The best way is to build up a class around operative system.
Hmmm,

could you explain that a bit...not exactly sure what you mean.
High!

In this case you must be sure of the file contents besides its size, name and extension.

regards
Thanks Duoas,

some interesting stuff to read.
Topic archived. No new replies allowed.