c++ - 将二进制数据写入 private ofstream 会产生意想不到的结果

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

最佳答案

你有:

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

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

关于c++ - 将二进制数据写入 private ofstream 会产生意想不到的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30825348/

相关文章:

c++ - 指针变量也分配了内存地址吗?

c++ - 多重继承 : unexpected result after cast from void * to 2nd base class

c++ - 引用计数的发布-消费排序

在包含该类列表的结构之后定义的 C++ 类

c++ - 在另一个类构造函数中初始化一个类对象

java - 如何在 OncreateMethod 中同时使用 AppCompatActivity 和 Activity 类

c++ - 无法使用初始化列表构造 vector <std::unique<...>>

c++ - 谷歌模拟 : mock fstream object and control of execution in test

c++ - 使用 fstream 向所有用户发送消息

用于读写的 C++ 二进制 fstream 不起作用