lundi 27 novembre 2017

How to write float vector to file using streams C++

I have the following structure

typedef std::vector<float> vectorf;
struct FM_Detector_params_t {
    float Fs; // Sampling frequency, Hz
    float Fc; // Carrier freq., Hz
    int Kd;   // Decimation coef
    int fir1_order;
    int fir2_order;
    vectorf fir1_coefs;
    vectorf fir2_coefs;

    int block_size;

    FM_Detector_params_t() {
        Fs = 44100.;
        Fc = 1800.;
        Kd = 12;
        fir1_coefs = vectorf();
        fir1_order = FIR1_ORDER;
        block_size = DEFAULT_BLOCK_SIZE;
        fir2_coefs = vectorf();
        fir2_order = FIR2_ORDER;
    };

};

I need to write it to the file, I have tried the following code to do this

Write to file

bool try_to_restore_filter_configuration() {
    std::ifstream in;
    FM_Detector_params_t restored;
    // Set invalid values to check later
    restored.Kd = INVALID_CONFIG_VALUE;
    restored.Fs = INVALID_CONFIG_VALUE;
    in.open(this->get_config_file_name(), std::ios::binary);
/*    if (in.good()) {*/
    in.read(reinterpret_cast<char *>(&restored),
            sizeof(FM_Detector_params_t));
    in.read(reinterpret_cast<char *>(&restored.Fs),
            sizeof(restored.Fs));
    in.read(reinterpret_cast<char *>(&restored.Fc),
            sizeof(restored.Fc));
    in.read(reinterpret_cast<char *>(&restored.Kd),
            sizeof(restored.Kd));
    in.read(reinterpret_cast<char *>(&restored.fir1_order),
            sizeof(restored.fir1_order));
    in.read(reinterpret_cast<char *>(&restored.fir2_order),
            sizeof(restored.fir2_order));
    vectorf fir_1_coeffs;
    fir_1_coeffs.resize((unsigned long) restored.fir1_order);
    in.read((char *) &fir_1_coeffs[0], restored.fir1_order * sizeof(fir_1_coeffs[0]));

    in.close();
    bool zeros_fir_1 = std::all_of(fir_1_coeffs.begin(), fir_1_coeffs.end(),
                                   [](float i) { return i == 0; });

    if (restored.Kd > 0 && restored.Fs > 0 && !zeros_fir_1) {
        this->m_fm_detector_params_ = restored;
        return true;
    }
    return false;
}

Read

void save_current_filter_configuration() {
    std::ofstream out;
    std::cout << "FILE PATH " << this->get_config_file_name() << endl;
    out.open(this->get_config_file_name(), std::ios::binary);
    out.write(reinterpret_cast<char *>(&this->m_fm_detector_params_.Fs),
              sizeof(this->m_fm_detector_params_.Fs));
    out.write(reinterpret_cast<char *>(&this->m_fm_detector_params_.Fc),
              sizeof(this->m_fm_detector_params_.Fc));
    out.write(reinterpret_cast<char *>(&this->m_fm_detector_params_.Kd),
              sizeof(this->m_fm_detector_params_.Kd));
    out.write(reinterpret_cast<char *>(&this->m_fm_detector_params_.fir1_order),
              sizeof(this->m_fm_detector_params_.fir1_order));
    out.write(reinterpret_cast<char *>(&this->m_fm_detector_params_.fir2_order),
              sizeof(this->m_fm_detector_params_.fir2_order));
    auto fir_1_vec_first = this->m_fm_detector_params_.fir1_coefs[0];
    out.write(reinterpret_cast<const char *>(&fir_1_vec_first),
              this->m_fm_detector_params_.fir1_order * sizeof(fir_1_vec_first));
    out.flush();
    out.close();
}

The problem is that I cannot serialize/deserialize the vector, it seems to write some data to the file, but it fails to read it back, reading zeroes or throwing SIGSEGV.

Please help to solve the problem, what is wrong with my code ?

THanks

Aucun commentaire:

Enregistrer un commentaire