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 21, 2010 at 9:06 PM
    #21
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,944
    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
    Never mind, got it

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

Products Discussed in

To Top