将std :: ofstream对象作为参数传递给类方法

时间:2019-07-18 11:07:06

标签: c++ ofstream

由于已经回答了前面的问题,传递std::ofstream对象作为函数参数的方法似乎是传递引用:std::ofstream&

虽然编译了此解决方案,但所得结果并不等同于在方法内创建std :: ofstream对象,然后调用write()。

以下代码未提供正确的输出:

main.cpp 中:

std::ofstream file(path + "output.stubs");
stub->writeRaw(file); //stub is a pointer to an object of class Stub
file.close();

Stub.cpp 中:

void Stub::writeRaw(std::ofstream& file) {
    file.write((char*)this, sizeof(*this));
}

通过将 Stub.cpp 都更改为:

void Stub::writeRaw(void) {
    std::ofstream file(path + "output.stubs");
    file.write((char*)this, sizeof(*this));
    file.close();
}

或将对象写到main中的文件中,而不是调用类方法。

对此行为的任何帮助将不胜感激!

编辑

Stub类的一些上下文:

Stub.hpp

#pragma once

#include <iostream>
#include <ios>
#include <fstream>

#include "constants.hpp"
#include "DataTypes.hpp"


class Stub {
private:
    StubHeader header;
    StubIntrinsicCoordinates intrinsic;
    StubPayload payload;

public:
    Stub(void);
    virtual ~Stub(void);

    StubHeader getHeader(void);
    StubIntrinsicCoordinates getIntrinsicCoordinates(void);
    StubPayload getPayload(void);

    void setHeader(StubHeader stub_header);
    void setIntrinsicCoordinates(StubIntrinsicCoordinates stub_intrinsic);
    void setPayload(StubPayload stub_payload);

    void print(void);
    void writeRaw(std::ofstream& file);
};

相关数据类型定义如下:

struct StubHeader {
    uint8_t bx;
    uint8_t nonant;
};

struct StubIntrinsicCoordinates {
    uint8_t strip;
    uint8_t column;
    int crossterm;
};

struct StubPayload {
    bool valid;
    int r;
    int z;
    int phi;
    int8_t alpha;
    int8_t bend;
    uint8_t layer;
    bool barrel;
    bool module;
};

编辑2

读取存根的(玩具)代码如下:

std::ifstream r(path + "output.stubs");
Stub s;
r.read((char*)&s, sizeof(s));
s.print();

仅一个存根被写入文件,因为这是对功能的测试。 Stub类的打印功能如下:

void Stub::print(void) {
    std::cout << "----- Header -----" << '\n';
    std::cout << "bx: " << std::dec << (int)header.bx << '\n';
    std::cout << "nonant: " << std::dec << (int)header.nonant << '\n';
    std::cout << "----- Intrinsic Coordinates -----" << '\n';
    std::cout << "strip: " << std::dec << (int)intrinsic.strip << '\n';
    std::cout << "column: " << std::dec << (int)intrinsic.column << '\n';
    std::cout << "crossterm: " << std::dec << (int)intrinsic.crossterm << '\n';
    std::cout << "----- Payload -----" << '\n';
    std::cout << "valid: " << std::boolalpha << payload.valid << '\n';
    std::cout << "r: " << std::dec << (int)payload.r << '\n';
    std::cout << "z: " << std::dec << (int)payload.z << '\n';
    std::cout << "phi: " << std::dec << (int)payload.phi << '\n';
    std::cout << "alpha: " << std::dec << (int)payload.alpha << '\n';
    std::cout << "bend: " << std::dec << (int)payload.bend << '\n';
    std::cout << "layer: " << std::dec << (int)payload.layer << '\n';
    std::cout << "barrel: " << std::boolalpha << payload.barrel << '\n';
    std::cout << "module: " << std::boolalpha << payload.module << "\n\n";
}

编辑3

为确保完整性和透明度,请在下面找到 main.cpp 的确切代码:

int main(int argc, char const *argv[]) {
    Geometry g;
    g.generateModuleLUTs();
    g.generateCorrectionLUTs();
    std::vector<std::array<Stub*, PAYLOAD_WIDTH> > all_stubs;
    std::vector<Module> modules = g.getData(); 
    for (int i = 0; i < LINK_NUMBER; i++) {
        LinkGenerator link_gen;
        LinkFormatter link_formatter(link_gen.run());
        StubFormatter stub_formatter(link_formatter.run(), i);
        std::array<Stub*, PAYLOAD_WIDTH> stubs = stub_formatter.run(modules);
        CoordinateCorrector coordinate_corrector(stubs);
        all_stubs.push_back(coordinate_corrector.run());
    }
    std::ofstream f(path + "output.stubs");
    all_stubs[0][0]->writeRaw(f);
    all_stubs[0][0]->print();
    std::ifstream r(path + "output.stubs");
    Stub s;
    r.read((char*)&s, sizeof(s));
    s.print();
    return 0;
}

1 个答案:

答案 0 :(得分:1)

代码中的错误是在构造file.close()对象以再次读取文件之前,我没有调用std::ifstream。这是意外行为的原因。

使用this将类写入文件似乎是有效的,尽管请务必小心并准确知道要写入文件的内容。

感谢所有发表评论并帮助回答此问题的人!