1. Welcome to Tacoma World!

    You are currently viewing as a guest! To get full-access, you need to register for a FREE account.

    As a registered member, you’ll be able to:
    • Participate in all Tacoma discussion topics
    • Communicate privately with other Tacoma owners from around the world
    • Post your own photos in our Members Gallery
    • Access all special features of the site

Reading Binary File C++

Discussion in 'Technology' started by tarheelfan_08, Mar 18, 2010.

  1. Mar 18, 2010 at 7:18 PM
    #1
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,946
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    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

    Code:
    [FONT=Calibri]#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();
        }
    
    
    [/FONT]

    CPP
    Code:
    
    
    Code:
    #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;
    }
    
     
  2. Mar 20, 2010 at 10:33 PM
    #2
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,946
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    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
    Code:
    #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
    Code:
    #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:
    Code:
    //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??
     
  3. Mar 21, 2010 at 12:22 AM
    #3
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,946
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    Oh and when I hover over make, model or color, it says "Expression must be modifiable lvalue"
     
  4. Mar 21, 2010 at 12:55 AM
    #4
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,946
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    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 ==========
     
  5. Mar 21, 2010 at 1:11 AM
    #5
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,946
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    Ok guys, I figured it out.

    Code:
    //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);
    
    }
    

    Code:
        // 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??
     
  6. Mar 21, 2010 at 8:30 AM
    #6
    Evil Monkey

    Evil Monkey There's an evil monkey in my truck

    Joined:
    Aug 8, 2007
    Member:
    #2352
    Messages:
    8,337
    Gender:
    Male
    First Name:
    Robert
    Escondido, CA
    Vehicle:
    07 4x4 DC SR5 TRD Off-road
    Weathertech front & rear mats, rear suspension TSB, Toytec AAL for TSB, Hi-Lift Jack, Bilstein 5100 & Toytec Adjustable coilovers, Built Right UCAs, KMC XD 795 Hoss Wheels, Definity Dakota MTs 285/75R16, Leer XR, Thule Tracker II & Thule MOAB basket
    Use the this keyword instead of Car:: inside your constructor. this is used to clarify to the compiler that the scope you're intending is the object's member variable vs. some other parameter. It's only valid when used inside a class. You wouldn't use the this keyword in a main.


    Car::Car(char make, char model, char color, int year, int mileage) {
    this.make = make;
    this.model = model;
    this.color = color;
    this.year = year;
    valid_mileage(mileage);

    }
     
  7. Mar 21, 2010 at 9:41 AM
    #7
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,946
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    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
    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();
        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
    Code:
    #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;
    }
    
     
  8. Mar 21, 2010 at 10:13 AM
    #8
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,946
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    When I add cout's to see what the program is doing it just runs them together and does nothing with the read.

    Code:
    #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;
    }
    
    
     
  9. Mar 21, 2010 at 10:36 AM
    #9
    NetMonkey

    NetMonkey Well-Known Member

    Joined:
    Aug 14, 2008
    Member:
    #8536
    Messages:
    1,725
    Gender:
    Male
    Geogetown, TX
    Vehicle:
    2010, 4x4, DC, off-road, shortbed, automatic
    Toytec Ultimate Lift @ 3", Mickey Thompson MTZ's 285/75/16, Moto Metal 955b, rear 2" ALL, Marlin Crawler sliders
    struct record r[3];
    // write three cars
    out.write( (char*)r, sizeof(struct record) * 3);

    the variable r does not contain any data. you never assigned anything to it.
     
  10. Mar 21, 2010 at 11:25 AM
    #10
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,946
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    so will I need to do r = car or r = car_array or what??
     
  11. Mar 21, 2010 at 11:38 AM
    #11
    NetMonkey

    NetMonkey Well-Known Member

    Joined:
    Aug 14, 2008
    Member:
    #8536
    Messages:
    1,725
    Gender:
    Male
    Geogetown, TX
    Vehicle:
    2010, 4x4, DC, off-road, shortbed, automatic
    Toytec Ultimate Lift @ 3", Mickey Thompson MTZ's 285/75/16, Moto Metal 955b, rear 2" ALL, Marlin Crawler sliders
    well... thats what you are going to need to figure out.
    how do you assign values from one data structure to another?
     
  12. Mar 21, 2010 at 11:40 AM
    #12
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,946
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    That I am not sure off to be honest!
     
  13. Mar 21, 2010 at 11:41 AM
    #13
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,946
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    Wait would it be this??

    for (int i = 0; i < SIZE; i++)
    {
    r.make = Car_array.getMake();
    r.model = Car_array.getModel();
    r.color = Car_array.getColor();
    r.year = Car_array.getYear();
    r.mileage = Car_array.getMileage();
    }
     
  14. Mar 21, 2010 at 11:45 AM
    #14
    NetMonkey

    NetMonkey Well-Known Member

    Joined:
    Aug 14, 2008
    Member:
    #8536
    Messages:
    1,725
    Gender:
    Male
    Geogetown, TX
    Vehicle:
    2010, 4x4, DC, off-road, shortbed, automatic
    Toytec Ultimate Lift @ 3", Mickey Thompson MTZ's 285/75/16, Moto Metal 955b, rear 2" ALL, Marlin Crawler sliders
    that looks like it would work.
     
  15. Mar 21, 2010 at 11:47 AM
    #15
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,946
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    Ok here is my code.

    Code:
    #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;
    }
    
    but when I try to open the .bin file it does not show as 1's and 0's and the program still does not read the file for some reason
     
  16. Mar 21, 2010 at 11:53 AM
    #16
    NetMonkey

    NetMonkey Well-Known Member

    Joined:
    Aug 14, 2008
    Member:
    #8536
    Messages:
    1,725
    Gender:
    Male
    Geogetown, TX
    Vehicle:
    2010, 4x4, DC, off-road, shortbed, automatic
    Toytec Ultimate Lift @ 3", Mickey Thompson MTZ's 285/75/16, Moto Metal 955b, rear 2" ALL, Marlin Crawler sliders
    // read 3 cars
    cout << "Reading file\n\n";
    ifstream in("binaryFile.bin", ios::binary);
    in.read( (char *)r, sizeof(struct record) * 3);

    just reading a file does not display the contents.
    how would you display the contents for the variable "in" ?

    also, when you open a binary file in a text editor, you will see hex values and not a collection of 1's and 0's.
    such as:
    http://en.wikipedia.org/wiki/Binary_file
     
  17. Mar 21, 2010 at 11:56 AM
    #17
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,946
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar

    You would do cout<<"The variable In is " << in << ".\n";

    So is their a way to find out if I am creating a binary file correctly or not??
     
  18. Mar 21, 2010 at 12:03 PM
    #18
    NetMonkey

    NetMonkey Well-Known Member

    Joined:
    Aug 14, 2008
    Member:
    #8536
    Messages:
    1,725
    Gender:
    Male
    Geogetown, TX
    Vehicle:
    2010, 4x4, DC, off-road, shortbed, automatic
    Toytec Ultimate Lift @ 3", Mickey Thompson MTZ's 285/75/16, Moto Metal 955b, rear 2" ALL, Marlin Crawler sliders
    this created a binary file:
    ofstream out("binaryFile.bin", ios::binary);
    // write three cars
    out.write( (char*)r, sizeof(struct record) * 3);
    out.close();


    you figured out how to transfer the values from the array to the struct. so how would you print the values from the variable r?
     
  19. Mar 21, 2010 at 4:33 PM
    #19
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,946
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    Would I do this??

    Code:
    // 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();
            }
    
    Or would this simply just b printing the array from the program and not the actual file??
     
  20. Mar 21, 2010 at 8:35 PM
    #20
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,946
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    Here is my code
    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();
    }
    
    Code:
    #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?
     
To Top