D. Monk
D. Monk

Reputation: 19

Passing std::ofstream object as argument to class method

As previous questions have been answered, the way to pass a std::ofstream object as a function argument seems to be to instead pass a reference: std::ofstream&.

Whilst this solution compiles, the resulting output is not equivalent to creating an std::ofstream object within the method then calling write().

The code below does not give the correct output:

In main.cpp:

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

In Stub.cpp:

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

The correct output is given by both changing Stub.cpp to:

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

or writing the object to the file in main instead of calling a class method.

Any help on this behaviour would be greatly appreciated!

EDIT

Some context for the class 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);
};

And the relevant data types are defined as follows:

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

EDIT 2

The (toy) code to read the stub is as follows:

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

Only one stub is written to the file as this was a test of functionality. The print function for the Stub class is as follows:

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

EDIT 3

For completeness and transparency, please find below the exact code for 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;
}

Upvotes: 0

Views: 1265

Answers (1)

D. Monk
D. Monk

Reputation: 19

The bug in the code was that I was not calling file.close() before constructing the std::ifstream object to read the file again. This was the cause of the unexpected behaviour.

Writing a class to file using this seems to be valid, although it is important that you are careful and know exactly what you want to write to a file.

Thank you to everyone who commented and helped to answer this question!

Upvotes: 1

Related Questions