Oops! I'm sorry for that. I added that to the Car class so that I could verify the program was working. Just delete that loop and line. But if you really want to keep it, here it is

void init()
    {
        memset(this, 0, sizeof(*this));
    }

Note that initializing a class like that may not always work correctly, and in many cases might actually destroy important data. Use this syntax very sparingly. As a general rule of thumb it is better to initialize each member object individually.

When I add that to the car class I get this error:

1>carclass.h(132): error C2355: 'this' : can only be referenced inside non-static member functions

#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();
	void init();
    string string_car_details();
};

//Sets to default values
Car::Car() 
{
make[0] = '\0';
model[0] = '\0';
color[0] = '\0';
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 init()
	{
		memset(this, 0, sizeof(*this));
	}

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();
}

When I add that to the car class I get this error:

1>carclass.h(132): error C2355: 'this' : can only be referenced inside non-static member functions

You did not make it an inline function -- in the implementation code you have to do like all other class methods

void Car::init()
{
  // blabla
}

Here is my code

#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();
	void init();
    string string_car_details();
};

//Sets to default values
Car::Car() 
{
make[0] = '\0';
model[0] = '\0';
color[0] = '\0';
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::init()
	{
		memset(this, 0, sizeof(*this));
	}

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

using namespace std;
      int main () {

      const int SIZE = 3;
      size_t x;

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

      cout << "Now wiring the data to the file! ";
	  ofstream out("binaryFile.bin", ios::binary);

      // write three cars
	  x = sizeof(Car_array);
	  out.write( (char*)Car_array, x);
      out.close();

	  //Tells if file can not be accessed
	  if (!out)
	  {
	  	cout << "Error Opening file. ";
		return 0;

	  }

	  cout << "Now reading the data back into memory! ";
      // read 3 cars
      for(int i = 0; i < 3; i++)
      Car_array[i].init();
      ifstream in("binaryFile.bin", ios::binary);
      in.read( (char*)Car_array, sizeof(Car_array));
      in.close();
      return 0;

      }

This program is supposed to write everything to the file and then print out the contents of the file and it is not printing anything!

Made an update to my previous post, I added information to tell if file can not be edited or opened!

Ok it runs but it still does not print the information from the file! How do I get it to print the information from the file!

Well, if you look at the last few lines in main() you will see that all it does is read from the file. You have to add more code to display the information. Create a short loop and display the value of each of the Cars.

Isn't that what this does

cout << "Now reading the data back into memory!\n\n ";
      // read 3 cars
      ifstream in("binaryFile.bin", ios::binary);
      in.read( (char*)Car_array, sizeof(Car_array));
      in.close();
	  for(int i = 0; i < 3; i++)
      Car_array[i].init();
      return 0;

Does this code not print out my information from the file??

No. All is does is read the file. Where do you see use of cout in that code, e.g. cout << Car_array[i].make; ???

And delete lines 6 and 7 that you posted. After reading the file you don't want to re-initialize the class because the init() method will destroy all the information that was read from the file.

Never mind, got it

#include "CarClass.h"
#include <fstream>
#include <sys/stat.h>
#include <string>


using namespace std;
      int main () {

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

    struct record r[3];

    for (int i = 0; i < 3; i++)
    {
        strcpy_s(r[0].make, "Ford");
        strcpy_s(r[0].model, "Mustang");
        strcpy_s(r[0].color, "Blue");
        r[0].year = 2008;
        r[0].mileage = 56789;

		strcpy_s(r[1].make, "Toyota");
        strcpy_s(r[1].model, "Tacoma");
        strcpy_s(r[1].color, "White");
        r[1].year = 2010;
        r[1].mileage = 10;

		strcpy_s(r[2].make, "Nissan");
        strcpy_s(r[2].model, "350Z");
        strcpy_s(r[2].color, "Orange");
        r[2].year = 2009;
        r[2].mileage = 112345;
    }

    ofstream out("binaryFile.dat", ios::binary | ios::trunc);
    out.exceptions ( ifstream::eofbit | ifstream::failbit | ifstream::badbit );

    if(!out)
        cout << "Can't open output file!" << endl;
    else {
        try {
            out.write((char *)r, sizeof(r) * 3);
        }
        catch(ofstream::failure e) {
            cout << "Exception writing file";
        }
        out.close();
    }

    // array of empty structures, file read fills them in
    struct record recordsReadFromFile[3];

    ifstream in("binaryFile.dat", ios::binary);

    if(!in) {
        cout << "Can't open binaryFile.dat" << endl;
    }
    else {
        in.read( (char *)recordsReadFromFile, sizeof(struct record) * 3);

        if (!in.good())
            cout << "Read of binaryFile.dat failed" << endl;
    }

    // show what was read from file
    for (int i = 0; i < 3; i++) {
        cout << recordsReadFromFile[i].make << ", "
            << recordsReadFromFile[i].model << ", "
            << recordsReadFromFile[i].color << ", "
            << recordsReadFromFile[i].year << ", "
            << recordsReadFromFile[i].mileage <<" miles on the v!ehicle "<< endl;
        cout << endl;
    }

    cout << "Press ENTER to continue...";
    cin.sync();
    cin.get();
    return 0;
}

And thanks for wasting all my time and others too here at DaniWeb. I see you also posted the same at dream.in.code.

Hey man I really appreciate your help, and the reason I posted on another site was to see how different people would fix my problem! I like to know multiple ways to do something rather then only knowing one way! So by posting on 2 sites I get to here from more people!

Hey man I really appreciate your help, and the reason I posted on another site was to see how different people would fix my problem! I like to know multiple ways to do something rather then only knowing one way! So by posting on 2 sites I get to here from more people!

You never post on more than one site without telling both sites that you're doing so. People on each site will give you the same advice, not knowing, and it's considered a breach of ettiquette. If you didn't know that before, you do now.

That I did not know, and I truly am sorry for doing this! Next time I have a problem or comment, I will make sure to note if I post on both sites!

That I did not know, and I truly am sorry for doing this! Next time I have a problem or comment, I will make sure to note if I post on both sites!

Apology accepted. :cool:

Hi, friends who have been involved in this discussion:

Nice to initialize a very interesting discussion. Once you have time, would you mind to give a short summary on the set of issues meet and solved in this thread. It should be very helpful to person like me, who are C++ beginners. I was trying to monitor the discussion of this thread, but lost somehow.

Hi, friends who have been involved in this discussion:

Nice to initialize a very interesting discussion. Once you have time, would you mind to give a short summary on the set of issues meet and solved in this thread. It should be very helpful to person like me, who are C++ beginners. I was trying to monitor the discussion of this thread, but lost somehow.

Some of the issues that I faced when creating this program was getting the array to write and read properly. I originally used the following array

const int SIZE = 3;
      size_t x;

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

But when I was told that the following would be easier I went with it.

for (int i = 0; i < 3; i++)
    {
        strcpy_s(r[0].make, "Ford");
        strcpy_s(r[0].model, "Mustang");
        strcpy_s(r[0].color, "Blue");
        r[0].year = 2008;
        r[0].mileage = 56789;

		strcpy_s(r[1].make, "Toyota");
        strcpy_s(r[1].model, "Tacoma");
        strcpy_s(r[1].color, "White");
        r[1].year = 2010;
        r[1].mileage = 10;

		strcpy_s(r[2].make, "Nissan");
        strcpy_s(r[2].model, "350Z");
        strcpy_s(r[2].color, "Orange");
        r[2].year = 2009;
        r[2].mileage = 112345;
    }

By using this array it was much easier to write and read to a binary file! And it read in a better format. Here is my final code in case you want to run it in a compiler and test and change stuff for your knowledge.

Class

#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();
	void init();
    string string_car_details();
};

//Sets to default values
Car::Car() 
{
make[0] = '\0';
model[0] = '\0';
color[0] = '\0';
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::init()
	{
		memset(this, 0, sizeof(*this));
	}

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

#include "CarClass.h"
#include <fstream>
#include <sys/stat.h>
#include <string>


using namespace std;
      int main () {

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

    struct record r[3];

    for (int i = 0; i < 3; i++)
    {
        strcpy_s(r[0].make, "Ford");
        strcpy_s(r[0].model, "Mustang");
        strcpy_s(r[0].color, "Blue");
        r[0].year = 2008;
        r[0].mileage = 56789;

		strcpy_s(r[1].make, "Toyota");
        strcpy_s(r[1].model, "Tacoma");
        strcpy_s(r[1].color, "White");
        r[1].year = 2010;
        r[1].mileage = 10;

		strcpy_s(r[2].make, "Nissan");
        strcpy_s(r[2].model, "350Z");
        strcpy_s(r[2].color, "Orange");
        r[2].year = 2009;
        r[2].mileage = 112345;
    }

    ofstream out("binaryFile.dat", ios::binary | ios::trunc);
    out.exceptions ( ifstream::eofbit | ifstream::failbit | ifstream::badbit );

    if(!out)
        cout << "Can't open output file!" << endl;
    else {
        try {
            out.write((char *)r, sizeof(r) * 3);
        }
        catch(ofstream::failure e) {
            cout << "Exception writing file";
        }
        out.close();
    }

    // array of empty structures, file read fills them in
    struct record recordsReadFromFile[3];

    ifstream in("binaryFile.dat", ios::binary);

    if(!in) {
        cout << "Can't open binaryFile.dat" << endl;
    }
    else {
        in.read( (char *)recordsReadFromFile, sizeof(struct record) * 3);

        if (!in.good())
            cout << "Read of binaryFile.dat failed" << endl;
    }

    // show what was read from file
    for (int i = 0; i < 3; i++) {
        cout << recordsReadFromFile[i].make << ", "
            << recordsReadFromFile[i].model << ", "
            << recordsReadFromFile[i].color << ", "
            << recordsReadFromFile[i].year << ", "
            << recordsReadFromFile[i].mileage <<" miles on the vehicle "<< endl;
        cout << endl;
    }

    cout << "Press ENTER to continue...";
    cin.sync();
    cin.get();
    return 0;
}

I hope this helps and if you have any more questions then feel free to ask. I can try to add more comments to my code if needed and other people can help you with questions you may have!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.