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

Writing binary data to private ofstream produces unexpected result

本文关键字:意外 结果 数据 二进制      更新时间:2023-10-16

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

导致这种情况的流有什么不同?

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

你有:

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

我不知道解决这些问题是否会解决您的所有问题。

我按原样复制了我的代码并将其保存在不同的文件中。问题解决了,但我不知道是什么原因造成的。