0

I have a sample file which reads contents from a file. The content is in an unreadable format as shown below. When I use the read() function and cout the variable, it returns the same binary data instead of showing normal text. How to read the contents in a humanly readable format?

Here are the contents of the file (upon using the write() function) I'm reading :

LÀ ³ L¹ R

Here is the reading code :

void readBinary(){
    ifstream inputFile("flights.dat", ios::in | ios::binary);

    char buffer[100];

    inputFile.seekg(0, ios::beg);

    if(inputFile.is_open()){

        inputFile.read((char *)&buffer, sizeof(buffer));

        for(int i = 0; i < sizeof(buffer); i++){
            cout << buffer[i];
        }

    }
    inputFile.close();
}

Here is the code for writing the data :

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

void inputData();

struct airplaneDetails {

    string airplaneCode;
    int totalRows, seatsInRows, firstSeats, businessSeats, economySeats;

};

int main(){

    inputData();

    return 0;
}

void inputData(){

    ofstream outputFile("flights.dat", ios::out | ios::binary | ios::app);

    if(!outputFile.is_open()){
        cout << "There was an error opening the file.";
    } else {

        airplaneDetails airplane;

        cout << "Please provide the details below :" << "\n\n";

        cout << "Enter the airplane code : ";
        cin >> airplane.airplaneCode;

        .
        .
        //Assigning values to the variables
        .
        .

        //Put the pointer at the end
        outputFile.seekp(0, ios::end);

        //Write the input data to the binary file
        outputFile.write((char *)&airplane.airplaneCode, sizeof(airplane.airplaneCode));
        outputFile.write((char *)&airplane.totalRows, sizeof(airplane.totalRows));
        outputFile.write((char *)&airplane.seatsInRows, sizeof(airplane.seatsInRows));
        outputFile.write((char *)&airplane.firstSeats, sizeof(airplane.firstSeats));
        outputFile.write((char *)&airplane.businessSeats, sizeof(airplane.businessSeats));
        outputFile.write((char *)&airplane.economySeats, sizeof(airplane.economySeats));

        //Close the file handler
        outputFile.close();
    }
}
16
  • 3
    Sorry but I'm not sure what you're expecting to see here, could you explain a bit further? Commented Apr 28, 2016 at 4:13
  • 5
    I'm surprised that you're surprised that when you read a file and print the data you just read, it prints the contents of the file. Commented Apr 28, 2016 at 4:14
  • Use inputFile.gcount() to determine exactly how many bytes were read from the file. Commented Apr 28, 2016 at 4:14
  • @vu1p3n0x I'm writing (using the write() function) text into this flights.dat file using another .cpp program. I want to read the contents from that .dat file but in a readable format. Commented Apr 28, 2016 at 4:14
  • 1
    @ShahlinIbrahim perhaps show us how you wrote the data to the file, it seems that's where the bug might be. Commented Apr 28, 2016 at 4:19

3 Answers 3

3

The problem is you know nothing about the internal representation of objects because their internals is hidden from you (its called "data hiding") so you have no idea what will happen if you dump their binary contents to a file.

You wrote binary out and you are getting binary in.

In particular the std::string does NOT contain the actual string which is why it is not getting written to your file. The std::string merely contains a pointer to the string that is stored elsewhere in memory.

Bottom line, don't do binary output unless you control and understand the contents of the objects being written.

You are better off converting everything to text and reading/writing that.

So rather than using read() and write() use operator>> and operator<<

bool output_details(const airplaneDetails& ad)
{
    std::ofstream ofs("flights.dat"); // text mode

    ofs << ad.airplaneCode << '\n';
    ofs << ad.totalRows << '\n';
    ofs << ad.seatsInRows << '\n';
    ofs << ad.firstSeats << '\n';
    ofs << ad.businessSeats << '\n';
    ofs << ad.economySeats << '\n';

    if(!ofs)
        return false;    

    return true;
}

bool input_details(airplaneDetails& ad)
{
    std::ifstream ifs("flights.dat"); // text mode

    ifs >> ad.airplaneCode;
    ifs >> ad.totalRows;
    ifs >> ad.seatsInRows;
    ifs >> ad.firstSeats;
    ifs >> ad.businessSeats;
    ifs >> ad.economySeats;

    if(!ifs)
        return false;    

    return true;
}
Sign up to request clarification or add additional context in comments.

Comments

0

To write text to a file, just write it the same way you'd write to cout:

outputFile << airplane.airplaneCode << endl;
outputFile << airplane.totalRows << endl;
// and so on

Comments

0

The problem was that I was trying to write a string to the binary file. When I used char airplaneCode[6] in my structure, it worked perfectly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.