Ok guys, for my next assignment I have to read from a binary file and then print out my information. I will post the assignment and what I have so far. But my question is, do I create the binary file first in the program or by hand or what?? I have read from a text file before but never a binary file.
1) Modify your program to read the initial list of cars in inventory from a data file that you create. It must read the saved cars into your array as it loads. The file must be stored in binary.
2) Once again. The file must be stored in binary.
3) The program must check for problems opening the binary data file and give an appropriate message to the user if it cannot open the file.
4) If the program doesn’t change the cars in the array in any way (I think they all do but just in case the one you select does not,) prompt the user for a change to the make and model of the second car in the array.
5) At the end of the program save the cars to the binary data file for the next program run.
Class
#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;
//Vehicle Class
class Car
{
protected:
string make; //make
string model; // model
string color; // color
int year; // year
int mileage; // miles on car
public:
//Constructor that will set information for a new car
void New_vehicle (string a, string b, string c, int d, int e)
{make = a; model = b; color = c; year = d; mileage = e;;}
Car(); //Default constructor
Car(string, string, string, int, int);
//mutator and accessor functions
void setMake(string);
void setModel(string);
void setColor(string);
void setYear(int);
void setMileage(int);
string getMake();
string getModel();
string 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(string make, string model, string color, int year, int mileage) {
Car::make = make;
Car::model = model;
Car::color = color;
Car::year = year;
valid_mileage(mileage);
}
void Car::setMake(string make) {
Car::make = make;
}
void Car::setModel(string model) {
Car::model = model;
}
void Car::setColor(string color) {
Car::color = color;
}
void Car::setYear(int year) {
Car::year = year;
}
void Car::setMileage(int mileage) {
valid_mileage(mileage);
}
string Car::getMake() {
return make;
}
string Car::getModel() {
return model;
}
string 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
#include "CarClass.h"
#include <fstream>
#include <sys/stat.h>
using namespace std;
ifstream::pos_type size;
char * memblock;
int main () {
ifstream file ("read.bin", ios::in|ios::binary|ios::ate);
if (file.is_open())
{
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();
cout << "the complete file content is in memory";
delete[] memblock;
}
else cout << "Unable to open file";
return 0;
}