Writing binary data to private ofstream produces unexpected result

137 views Asked by At

I am working on a class that will read and write binary data to/from a file. I am testing it by sending an 'a'. When I sent it to cout and it worked. I sent it to a text file, it sent î.

What is different about the ofstream that is causing this?

#include <iostream>
#include "bin2.h"

using namespace std;

int main()
{
    bin myBin("e:\\Temp\\test.txt");
    char data[1];
    data[0] = 'a';

    myBin.write(data, 1);

    system("PAUSE");
    return 0;
}

bin2.h

#pragma once
#include <fstream>

class bin
{
    std::ofstream outfile;
    std::ifstream infile;
    std::filebuf *outBuff, *inBuff;

    int buffSize = 5;
    char* buffer;

    //0 = input, 1 = output, 2 = ready to delete, 3 = unitialized
    char mode = 3;

public:
    //constructor with no parameters
    bin(){ ; };

    //if 'this' is constructed with a file, call init() to set object up
    bin(char fileName[])
    {
        init(fileName);
    };

    void init(char fileName[])
    {
        try
        {
            //check if isUninitialized
            if (!isUninitialized())
                return;

            //open the file and make sure it opened
            outfile.open(fileName);
            infile.open(fileName);
            if (!outfile.is_open() || !infile.is_open())
                throw std::runtime_error((std::string)"Failed to open file " + fileName);

            //create buffer, get pointers to filebuffs, and set them to the new buffer
            buffer = new char[buffSize];

            outBuff = outfile.rdbuf();
            outBuff->pubsetbuf(buffer, buffSize);

            inBuff = infile.rdbuf();            
            inBuff->pubsetbuf(buffer, buffSize);

            //set mode to input
            mode = 0;
            return
        }
        //if any exceptions were thrown, call the cleanup then rethrow the exception so
        //  the caller can handle the error as well
        catch (std::exception & ex) {
            cleanup();
            throw ex;
        }
    };

    virtual ~bin(){
        cleanup();
    };

    //methods to check mode
    bool modeIsInput(){ return mode == 0; };
    bool modeIsOutput(){ return mode == 1; };
    bool isReadyToDel(){ return mode == 2; };
    bool isUninitialized(){ return mode == 3; };
    std::string getMode(){
        switch (mode) {
        case 0: return "input";
        case 1: return "output";
        case 2: return "readyToDel";
        case 3: return "unitialized";
        default: return "invalid";
        }
    };

    //method to write data into the object
    bin * write(char data[], int length){
        //make sure object is in input mode
        if (mode != 0)
            throw std::runtime_error("Cannot write to object when not in input mode. Current mode = " + getMode());

        //DEBUG
        std::cout << "Writing data: ";
        std::cout.write(data, length);
        std::cout << std::endl;
        //end of DEBUG

        //write data and return pointer to object
        outfile.write(data, length);
        return this;
    };

private:
    void cleanup()
    {
        delete buffer;

        outfile.close();
        infile.close();

        //change mode to readyToDel
        mode = 2;
    };
};
2

There are 2 answers

0
Michael On

I copied my code as is and saved it it in a different file. The problem is solved, but I have no idea what caused it.

2
R Sahu On

You have:

bool modeIsInput(){ return mode = 0 ? true : false; };
bool modeIsOutput(){ return mode = 1 ? true : false; };
bool isReadyToDel(){ return mode = 2 ? true : false; };
bool isUninitialized(){ return mode = 3 ? true : false; };

I am sure you meant:

bool modeIsInput(){ return mode == 0 ? true : false; };
bool modeIsOutput(){ return mode == 1 ? true : false; };
bool isReadyToDel(){ return mode == 2 ? true : false; };
bool isUninitialized(){ return mode == 3 ? true : false; };

or better still.

bool modeIsInput(){ return mode == 0; };
bool modeIsOutput(){ return mode == 1; };
bool isReadyToDel(){ return mode == 2; };
bool isUninitialized(){ return mode == 3; };

I don't know whether fixing those will fix all your problems.