Reading and writing binary file

Ok guys, I spent all of yesterday understanding a binary file and creating a binary file's and I have managed to understand them pretty well and found some great websites explaining them. I have finished my code and it does not work, but I know whats wrong, I just need to know how to fix it!! I will post the code and then post my problem at the end!

Class
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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;

//Vehicle Class
class Car
{
protected:
		char make[40];	//Make of car
      	char model[40];	//Model of car
      	char color[20];	//Color of car
      	int year;		//Year of car
      	int mileage;	//Mileage of car

public:

	Car(); //Default constructor
	Car(char, char, char, int, int);
	//mutator and accessor functions
	void setMake(char);
    void setModel(char);
    void setColor(char);
    void setYear(int);
	void setMileage(int);


    char getMake();
    char getModel();
    char getColor();
    int getYear();
    int getMileage();


	//Check mileage to see if valid
    void valid_mileage(int);
    void car_details();
    string string_car_details();
};

//Sets to default values
Car::Car() {
	make = " ";
    model = " ";
    color = " ";
    year = 0;
    mileage = 0;
}
	// My Vehicle set up(Make, model, color, year, type,bedsize, bike, mileage)
Car::Car(char, char model, char color, int year, int mileage) {
	Car::make =  make;
	Car::model = model;
	Car::color = color;
	Car::year = year;
    valid_mileage(mileage);

}

void Car::setMake(char make) {
    Car::make = make;
}

void Car::setModel(char model) {
    Car::model = model;
}

void Car::setColor(char color) {
    Car::color = color;
}

void Car::setYear(int year) {
    Car::year = year;
}

void Car::setMileage(int mileage) {
    valid_mileage(mileage);
}


char Car::getMake() {
    return make;
}
char Car::getModel() {
    return model;
}
char Car::getColor() {
    return color;
}
int Car::getYear() {
    return year;
}
int Car::getMileage() {
    return mileage;
}


void Car::valid_mileage(int mileage) {
    if (mileage>=0)
        Car::mileage=mileage;
    else {
        Car::mileage=0;
        cout << "WARNING! You have entered invalid mileage!\n";
	}
    }

	void Car::car_details() {
	    cout << "The current car is a " << year << ' ' << color << ' '
			<< make << ' ' << model << " with " << mileage << " miles.\n\n";
	}



	string Car::string_car_details() {
	    stringstream buf;
	    buf << "The current car is a " << year << ' ' << color << ' '
	    << make << ' ' << model << " with " << mileage << " miles.\n\n";
	    return buf.str();
	}


CPP File
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
#include "CarClass.h"
#include <fstream>
#include <sys/stat.h>

using namespace std;

ifstream::pos_type size;
char * memblock;

int main () {

	struct record
	{

      char make[40];
      char model[40];
      char color[20];
      int year;
      int mileage;
      };


	const int SIZE = 3;
			
	//Array of 9 cars
	Car Car_array[SIZE] = { Car("Porsche", "911", "Silver", 2005, 18990), 
							Car("Ford", "Mustang", "Red", 2007, 49842),
							Car("Chevrolet", "Beretta", "Black", 1989, 90332)};


      ofstream out("filename.bin", ios::binary);
      struct record r[3];
      // write three cars
	  out.write( (char*)r, sizeof(struct record) * 3);

      // read 3 cars
      ifstream in("filename.bin", ios::binary);
      in.read( (char *)r, sizeof(struct record) * 3);
  return 0;
}


PROBLEM:
1
2
3
4
5
6
7
8
//Sets to default values
Car::Car() {
	make = " ";
    model = " ";
    color = " ";
    year = 0;
    mileage = 0;
}


What will I set the char's make, model, and color = to??

when I hover over make, model or color, it says "Expression must be modifiable lvalue" I also get other errors but I know these are all related to this error and when i figure out how to do the char, it will work!
As make, model and color are char arrays, which you are going to use as string:
eg "Ford", "Cortina", "Blue" and you want to initialise them to empty strings, then you could just do this:

1
2
3
4
5
6
7
Car::Car() {
	make[0]='\0';
    model[0]='\0' ;
    color[0]='\0';
    year = 0;
    mileage = 0;
}


All your get and set functions such as void setModel(char) and char getModel(); are wrong.
1>------ Build started: Project: FileReading, Configuration: Debug Win32 ------
1> Car.cpp
1>\filereading\carclass.h(48): error C2440: '=' : cannot convert from 'const char [14]' to 'char [40]'
1> There is no context in which this conversion is possible
1>\filereading\carclass.h(49): error C2440: '=' : cannot convert from 'const char [14]' to 'char [40]'
1> There is no context in which this conversion is possible
1>\filereading\carclass.h(50): error C2440: '=' : cannot convert from 'const char [14]' to 'char [20]'
1> There is no context in which this conversion is possible
1>\filereading\carclass.h(56): error C2106: '=' : left operand must be l-value
1>\filereading\carclass.h(57): error C2440: '=' : cannot convert from 'char' to 'char [40]'
1> There are no conversions to array types, although there are conversions to references or pointers to arrays
1>\filereading\carclass.h(58): error C2440: '=' : cannot convert from 'char' to 'char [20]'
1> There are no conversions to array types, although there are conversions to references or pointers to arrays
1>\filereading\carclass.h(65): error C2440: '=' : cannot convert from 'char' to 'char [40]'
1> There are no conversions to array types, although there are conversions to references or pointers to arrays
1>\filereading\carclass.h(69): error C2440: '=' : cannot convert from 'char' to 'char [40]'
1> There are no conversions to array types, although there are conversions to references or pointers to arrays
1>\filereading\carclass.h(73): error C2440: '=' : cannot convert from 'char' to 'char [20]'
1> There are no conversions to array types, although there are conversions to references or pointers to arrays
1>\filereading\carclass.h(86): error C2440: 'return' : cannot convert from 'char [40]' to 'char'
1> There is no context in which this conversion is possible
1>\filereading\carclass.h(89): error C2440: 'return' : cannot convert from 'char [40]' to 'char'
1> There is no context in which this conversion is possible
1>\filereading\carclass.h(92): error C2440: 'return' : cannot convert from 'char [20]' to 'char'
1> There is no context in which this conversion is possible
1>\filereading\car.cpp(24): error C2665: 'Car::Car' : none of the 3 overloads could convert all the argument types
1> \filereading\carclass.h(24): could be 'Car::Car(char,char,char,int,int)'
1> while trying to match the argument list '(const char [8], const char [4], const char [7], int, int)'
1>\filereading\car.cpp(25): error C2665: 'Car::Car' : none of the 3 overloads could convert all the argument types
1> \filereading\carclass.h(24): could be 'Car::Car(char,char,char,int,int)'
1> while trying to match the argument list '(const char [5], const char [8], const char [4], int, int)'
1>\filereading\car.cpp(26): error C2665: 'Car::Car' : none of the 3 overloads could convert all the argument types
1> \filereading\carclass.h(24): could be 'Car::Car(char,char,char,int,int)'
1> while trying to match the argument list '(const char [10], const char [8], const char [6], int, int)'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Ok guys, I figured it out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Sets to default values
Car::Car() {
    strcpy (make, "UNINITIALIZED");
    strcpy (model, "UNINITIALIZED");
    strcpy (color, "UNINITIALIZED");
    year = 0;
    mileage = 0;
}
    // My Vehicle set up(Make, model, color, year, type,bedsize, bike, mileage)
Car::Car(char make, char model, char color, int year, int mileage) {
    Car::make =  make;
    Car::model = model;
    Car::color = color;
    Car::year = year;
    valid_mileage(mileage);

}



1
2
3
4
5
6
7
8
9
    // My Vehicle set up(Make, model, color, year, type,bedsize, bike, mileage)
Car::Car(char make, char model, char color, int year, int mileage) {
    Car::make =  make;
    Car::model = model;
    Car::color = color;
    Car::year = year;
    valid_mileage(mileage);

}

But now I am getting errors with the second part! Does anyone know if I will have to do something similar or what??
Just about all these erros are because you are using char when you should be using char* for the function calls
Last edited on
huh?? so I need to add the * after all of my char's??
Just about- Like this.
Note:
1. I have made no buffer overflow check when copying the arrays - but you should.
2. We can do away with a default constructor if we give all the parameters in the other constructor all default values.
3. We can make the get functions constant

4. As you can probably guess, it is easier to use theC++ string type rather than char arrays.

With regard to points 2 and 3 - have you learnt about default parameter values or constant member functions yet??

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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>

#include <sys/stat.h>


using namespace std;

//Vehicle Class
class Car
{
protected:
    char make[40];	//Make of car
    char model[40];	//Model of car
    char color[20];	//Color of car
    int year;		//Year of car
    int mileage;	//Mileage of car

public:

    Car(); //Default constructor
    Car(char* make, char* model, char* color, int, int);
    //mutator and accessor functions
    void setMake(char*);
    void setModel(char*);
    void setColor(char*);
    void setYear(int);
    void setMileage(int);


    const char* getMake();
    const char* getModel();
    const char* getColor();
    int getYear();
    int getMileage();


    //Check mileage to see if valid
    void valid_mileage(int);
    void car_details();
    string string_car_details();
};

//Sets to default values
Car::Car() 
{
    strcpy(make,"Uninitialzed");
    strcpy(model,"Uninitialzed");
    strcpy(color,"Uninitialzed");
    year = 0;
    mileage = 0;
}
// My Vehicle set up(Make, model, color, year,  mileage)
Car::Car(char* make, char* model, char* color , int year, int mileage) 
{
    strcpy(Car::make,make);
    strcpy (Car::model,model);
    strcpy(Car::color,color);
    Car::year = year;
    valid_mileage(mileage);

}

void Car::setMake(char* make) 
{
    strcpy(Car::make,make);
}

void Car::setModel(char* model) 
{
    strcpy(Car::model,model);
}

void Car::setColor(char* color) 
{
    strcpy(Car::color,color);
}

void Car::setYear(int year) 
{
    Car::year = year;
}

void Car::setMileage(int mileage) 
{
    valid_mileage(mileage);
}


const char* Car::getMake()  
{
    return make;
}
const char* Car::getModel() 
{
    return model;
}
const char* Car::getColor() 
{
    return color;
}
int Car::getYear() 
{
    return year;
}
int Car::getMileage() 
{
    return mileage;
}


void Car::valid_mileage(int mileage) 
{
    if (mileage>=0)
        Car::mileage=mileage;
    else 
    {
        Car::mileage=0;
        cout << "WARNING! You have entered invalid mileage!\n";
    }
}

void Car::car_details() 
{
    cout << "The current car is a year" << year << ' ' << color << ' '
        << make << ' ' << model << " with " << mileage << " miles.\n\n";
}



string Car::string_car_details() 
{
    stringstream buf;
    buf << "The current car is a year " << year << ' ' << color << ' '
        << make << ' ' << model << " with " << mileage << " miles.\n\n";
    return buf.str();
}
Ok here is my new code guys, and it is creating the file that I want! But its not reading from that file and printing on the screen! IT should write the array of cars to the file, read the array of cars and print them on the screen and then re-write the array of cars..

Also, should I save this as a .bin file or a .dat file or .txt file or what??
CLASS
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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>

#include <sys/stat.h>


using namespace std;

//Vehicle Class
class Car
{
protected:
    char make[40];	//Make of car
    char model[40];	//Model of car
    char color[20];	//Color of car
    int year;		//Year of car
    int mileage;	//Mileage of car

public:

    Car(); //Default constructor
    Car(char* make, char* model, char* color, int, int);
    //mutator and accessor functions
    void setMake(char*);
    void setModel(char*);
    void setColor(char*);
    void setYear(int);
    void setMileage(int);


    const char* getMake();
    const char* getModel();
    const char* getColor();
    int getYear();
    int getMileage();


    //Check mileage to see if valid
    void valid_mileage(int);
    void car_details();
    string string_car_details();
};

//Sets to default values
Car::Car() 
{
    strcpy(make,"Uninitialzed");
    strcpy(model,"Uninitialzed");
    strcpy(color,"Uninitialzed");
    year = 0;
    mileage = 0;
}
// My Vehicle set up(Make, model, color, year,  mileage)
Car::Car(char* make, char* model, char* color , int year, int mileage) 
{
    strcpy(Car::make,make);
    strcpy (Car::model,model);
    strcpy(Car::color,color);
    Car::year = year;
    valid_mileage(mileage);

}

void Car::setMake(char* make) 
{
    strcpy(Car::make,make);
}

void Car::setModel(char* model) 
{
    strcpy(Car::model,model);
}

void Car::setColor(char* color) 
{
    strcpy(Car::color,color);
}

void Car::setYear(int year) 
{
    Car::year = year;
}

void Car::setMileage(int mileage) 
{
    valid_mileage(mileage);
}


const char* Car::getMake()  
{
    return make;
}
const char* Car::getModel() 
{
    return model;
}
const char* Car::getColor() 
{
    return color;
}
int Car::getYear() 
{
    return year;
}
int Car::getMileage() 
{
    return mileage;
}


void Car::valid_mileage(int mileage) 
{
    if (mileage>=0)
        Car::mileage=mileage;
    else 
    {
        Car::mileage=0;
        cout << "WARNING! You have entered invalid mileage!\n";
    }
}

void Car::car_details() 
{
    cout << "The current car is a year" << year << ' ' << color << ' '
        << make << ' ' << model << " with " << mileage << " miles.\n\n";
}



string Car::string_car_details() 
{
    stringstream buf;
    buf << "The current car is a year " << year << ' ' << color << ' '
        << make << ' ' << model << " with " << mileage << " miles.\n\n";
    return buf.str();
}


CPP File
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
#include "CarClass.h"
#include <fstream>
#include <sys/stat.h>

using namespace std;


int main () {

	struct record
	{

      char make[40];
      char model[40];
      char color[20];
      int year;
      int mileage;
      };


	const int SIZE = 3;
			
	//Array of 9 cars
	Car Car_array[SIZE] = { Car("Porsche", "911", "Silver", 2005, 18990), 
							Car("Ford", "Mustang", "Red", 2007, 49842),
							Car("Chevrolet", "Beretta", "Black", 1989, 90332)};


      ofstream out("binaryFile.bin", ios::binary);
      struct record r[3];
      // write three cars
	  out.write( (char*)r, sizeof(struct record) * 3);

      // read 3 cars
      ifstream in("binaryFile.bin", ios::binary);
      in.read( (char *)r, sizeof(struct record) * 3);
  return 0;
}
you did not close the ofstream :(
i mean out.close();
more on this here.. http://cplusplus.com/doc/tutorial/files/
Will I just do ofstram::close()

Sorry saw this after your post

And will i need to close the ifstrream??
Last edited on
Even now that its closed its not printing the array to the screen!
well there are no cout statements on your main()
You mean like cout <<"Writing to file";

cout << "Reading file";
When I add cout's to see what the program is doing it just runs them together and does nothing with the read.

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
#include "CarClass.h"
#include <fstream>
#include <sys/stat.h>

using namespace std;


int main () {

	struct record
	{

      char make[40];
      char model[40];
      char color[20];
      int year;
      int mileage;
      };


	const int SIZE = 3;
			
	//Array of 9 cars
	Car Car_array[SIZE] = { Car("Porsche", "911", "Silver", 2005, 18990), 
							Car("Ford", "Mustang", "Red", 2007, 49842),
							Car("Chevrolet", "Beretta", "Black", 1989, 90332)};

	cout << "Writing to file\n\n";
      ofstream out("binaryFile.bin", ios::binary);
      struct record r[3];
      // write three cars
	  out.write( (char*)r, sizeof(struct record) * 3);
	  out.close();


      // read 3 cars
	  cout << "Reading file\n\n";
      ifstream in("binaryFile.bin", ios::binary);
      in.read( (char *)r, sizeof(struct record) * 3);
  return 0;
}
Topic archived. No new replies allowed.