Reading and writing binary file c++

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 1803 Views - Last Post: 21 March 2010 - 09:13 PM Rate Topic: -----

#16 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Reading and writing binary file c++

Posted 21 March 2010 - 10:27 AM

...
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++) {
	cout << r[i].make << endl
             << r[i].model << endl
	     << r[i].color << endl
	     << r[i].year << endl
	     << r[i].mileage << endl;
}
...



Produces:

╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠Nw>~╕ ↕
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠Nw>~╕ ↕
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠Nw>~╕ ↕
-858993460
-858993460
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠Nw>~╕ ↕
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠Nw>~╕ ↕
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠Nw>~╕ ↕
-858993460
-858993460
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠Nw>~╕ ↕
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠Nw>~╕ ↕
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠Nw>~╕ ↕
-858993460
-858993460


This post has been edited by n8wxs: 21 March 2010 - 10:28 AM

Was This Post Helpful? 0
  • +
  • -

#17 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Reading and writing binary file c++

Posted 21 March 2010 - 11:24 AM

Then what am I doing wrong?? I mean what will give me the 100101011100's
Was This Post Helpful? 0
  • +
  • -

#18 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Reading and writing binary file c++

Posted 21 March 2010 - 11:29 AM

I did this
#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;
};

struct record r[3];

for (int i = 0; i < 3; i++) {
        cout << r[i].make << endl
             << r[i].model << endl
             << r[i].color << endl
             << r[i].year << endl
             << r[i].mileage << endl;
}


	const int SIZE = 3;
			
	//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 << "Writing to file\n\n";
      ofstream out("binaryFile.bin", ios::binary);
      // 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;
}




And it gives me exactly what you said! But how do I fix it or what have I done wrong??
Was This Post Helpful? 0
  • +
  • -

#19 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Reading and writing binary file c++

Posted 21 March 2010 - 11:37 AM

You didn't initialize struct variables with anything. Take a look here:
#include "CarClass.h"
#include <fstream>
#include <sys/stat.h>

using namespace std;


int main () 
{
	struct record
	{
            // since getMake, getModel and getColor returns a const char *
           // make these const char * too
            const char *make; 
            const char *model;
            const char *color;
            int year;
            int mileage;
	};

	struct record r[3];

        const int SIZE = 3;
                        
        //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)};

        // Based on Car_array initialize everything
	for (int i = 0; i < SIZE; i++)
	{
		r[i].make = Car_array[i].getMake();
		r[i].model = Car_array[i].getModel();
		r[i].color = Car_array[i].getColor();
		r[i].year = Car_array[i].getYear();
		r[i].mileage = Car_array[i].getMileage();
	}

	for (int i = 0; i < 3; i++) 
	{
        cout << r[i].make << endl
             << r[i].model << endl
             << r[i].color << endl
             << r[i].year << endl
             << r[i].mileage << endl << endl;
	}

        cout << "Writing to file\n\n";
      ofstream out("binaryFile.bin", ios::binary);
      // 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;
}


SOrry for bad indentation but I have no time to fix it ...

This post has been edited by sarmanu: 21 March 2010 - 11:38 AM

Was This Post Helpful? 0
  • +
  • -

#20 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Reading and writing binary file c++

Posted 21 March 2010 - 11:42 AM

Ok thanks for that...but I have just one question and this may be stupid! But why can I ot just assign r = Car_array??
Was This Post Helpful? 0
  • +
  • -

#21 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Reading and writing binary file c++

Posted 21 March 2010 - 11:46 AM

First of all, "r" and "Car" are different data types.
Secondly, the Compiler is not human. You need to explicitely tell it what to do. Think about, let's suppose that you were able to use:
r = Car_array;


How would the Compiler know that you want to use:
r[i].make = Car_array[i].getMake();


??
What if the Compiler want to do this:
r[i].make = Car_array[i].getModel();


Both getMake and getModel returns a const char *.
DAMN, great thing you can't just assign like that ...

This post has been edited by sarmanu: 21 March 2010 - 11:47 AM

Was This Post Helpful? 1
  • +
  • -

#22 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Reading and writing binary file c++

Posted 21 March 2010 - 11:58 AM

Ah thanks for explaining that! But one final question, my output still does not read the file and then print the contents. Will I need to change anything or do a cout statement or what??
Was This Post Helpful? 0
  • +
  • -

#23 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Reading and writing binary file c++

Posted 21 March 2010 - 04:35 PM

To get it to print the contents of the file, would I do this??

// read 3 cars
          cout << "Reading file\n\n";
      ifstream in("binaryFile.dat", ios::binary);
      in.read( (char *)r, sizeof(struct record) * 3);
	  // Based on Car_array initialize everything
        for (int i = 0; i < SIZE; i++)
        {
                r[i].make = Car_array[i].getMake();
                r[i].model = Car_array[i].getModel();
                r[i].color = Car_array[i].getColor();
                r[i].year = Car_array[i].getYear();
                r[i].mileage = Car_array[i].getMileage();
        }


Was This Post Helpful? 0
  • +
  • -

#24 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Reading and writing binary file c++

Posted 21 March 2010 - 05:27 PM

No.

If r has been filled with the data read from the file, why would you copy Car_array[] data to it to print what you just read from the file??

This post has been edited by n8wxs: 21 March 2010 - 05:28 PM

Was This Post Helpful? 0
  • +
  • -

#25 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Reading and writing binary file c++

Posted 21 March 2010 - 07:04 PM

Well then, what will I do to get it to read from the file??
Was This Post Helpful? 0
  • +
  • -

#26 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Reading and writing binary file c++

Posted 21 March 2010 - 08:35 PM

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! Can someone help me figure out why?
Was This Post Helpful? 0
  • +
  • -

#27 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Reading and writing binary file c++

Posted 21 March 2010 - 08:46 PM

Here's one way to write and read binary records to/from a file:

// diccppca1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    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[i].make, "Ford");
        strcpy_s(r[i].model, "ThunderBird");
        strcpy_s(r[i].color, "Bright Blue!");
        r[i].year = 1234;
        r[i].mileage = 56789;
    }

    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;

        in.close();
    }

    // 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 << endl;
        cout << endl;
    }

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



Produces:

Quote

Ford, ThunderBird, Bright Blue!, 1234, 56789

Ford, ThunderBird, Bright Blue!, 1234, 56789

Ford, ThunderBird, Bright Blue!, 1234, 56789

Press ENTER to continue...

This post has been edited by n8wxs: 21 March 2010 - 08:52 PM

Was This Post Helpful? 1
  • +
  • -

#28 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Reading and writing binary file c++

Posted 21 March 2010 - 08:54 PM

Wow dude that is a lot simpliar then what I was trying to do, but now I got one question! If I want to have 3 different cars then what will I do! Because if I was to use what you have then it will just print the Thunderbird 3 times!
Was This Post Helpful? 0
  • +
  • -

#29 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Reading and writing binary file c++

Posted 21 March 2010 - 09:00 PM

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



Was This Post Helpful? 0
  • +
  • -

#30 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Reading and writing binary file c++

Posted 21 March 2010 - 09:01 PM

NO IT WON'T!

I simply loaded the r[] with the same data. If you copy your Car_array[] data to the array before you write it, that's what you will read when you read the file!
Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3