将二进制数据写入私有流会产生意外结果

时间:2015-06-14 02:10:50

标签: c++ class fstream binary-data

我正在研究一个可以从文件读取和写入二进制数据的类。 我正在通过发送'a'来测试它。当我将它发送到cout并且它有效。我把它发送到一个文本文件,它发送了î。

造成这种情况的ofstream有什么不同?

#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 个答案:

答案 0 :(得分:1)

你有:

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

我相信你的意思是:

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

或更好。

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

我不知道修复这些问题是否能解决所有问题。

答案 1 :(得分:0)

我按原样复制了我的代码并将其保存在另一个文件中。问题解决了,但我不知道是什么导致了它。