Search code examples
c++filestream

Am I concentrating these character arrays incorrectly?


I've been trying to create a midi file from scratch with c++. I'm separating different chunks(and parts of chunks) into different character arrays.

My code:

#include <iostream>
#include <fstream>
#include <cstring>

int main(int, char* []) {
    std::ofstream MIDIfile ("example.mid")
    char header[] = { /* hex data */ };
    char track_header[] = { /* hex data */ };
    char track_data[] = { /* hex data */ };
    MIDIfile << strcat(header, strcat(track_header, track_data));
    MIDIfile.close();
    return 0;
}

My only problem is that when the file is written, only 8 of the 81 bytes are written. Is there a reason for this? Am I doing something wrong?

Regards, shadowstalker75


Solution

  • You'll have to learn what strcat() does. This line is never going to work. Actually, even better, NEVER USE strcat(). It's crap.

    MIDIfile << strcat(header, strcat(track_header, track_data));
    

    You have binary buffers of hex data, just use the write() function:

    MIDIfile.write(header, sizeof(header));
    ...
    

    and write one buffer at a time.