как правильно записать данные в двоичном формате: #include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct dataFreq {
    char data;
    int freq;
};
struct codeTable {
    char data;
    string code;
};
struct minHeapNode {
    dataFreq* df;
    minHeapNode* left, * right;
};
struct minHeap {
    int size;
    int capacity;
    minHeapNode** arr;
};
minHeapNode* newNode(dataFreq* df) {
    minHeapNode* temp = new minHeapNode;
    temp->left = temp->right = nullptr;
    temp->df = df;
    return temp;
}
minHeap* createMinHeap(int capacity) {
    minHeap* mh = new minHeap;
    mh->size = 0;
    mh->capacity = capacity;
    mh->arr = new minHeapNode * [capacity];
    return mh;
}
void swapMinHeapNode(minHeapNode** a, minHeapNode** b) {
    minHeapNode* t = *a;
    *a = *b;
    *b = t;
}
void minHeapify(minHeap* mh, int idx) {
    int smallest = idx;
    int left = 2 * idx + 1;
    int right = 2 * idx + 2;
    if (left < mh->size && mh->arr[left]->df->freq < mh->arr[smallest]->df->freq) {
        smallest = left;
    }
    if (right < mh->size && mh->arr[right]->df->freq < mh->arr[smallest]->df->freq) {
        smallest = right;
    }
    if (smallest != idx) {
        swapMinHeapNode(&mh->arr[smallest], &mh->arr[idx]);
        minHeapify(mh, smallest);
    }
}
minHeapNode* extractMin(minHeap* mh) {
    minHeapNode* temp = mh->arr[0];
    mh->arr[0] = mh->arr[mh->size - 1];
    --mh->size;
    minHeapify(mh, 0);
    return temp;
}
void insertMinHeap(minHeap* mh, minHeapNode* mhNode) {
    ++mh->size;
    int i = mh->size - 1;
    while (i && mhNode->df->freq < mh->arr[(i - 1) / 2]->df->freq) {
        mh->arr[i] = mh->arr[(i - 1) / 2];
        i = (i - 1) / 2;
    }
    mh->arr[i] = mhNode;
}
void buildMinHeap(minHeap* mh) {
    int n = mh->size - 1;
    for (int i = (n - 1) / 2; i >= 0; i--) {
        minHeapify(mh, i);
    }
}
minHeap* createAndBuildMinHeap(dataFreq* df, int size) {
    minHeap* mh = createMinHeap(size);
    for (int i = 0; i < size; i++) {
        mh->arr[i] = newNode(&df[i]);
    }
    mh->size = size;
    buildMinHeap(mh);
    return mh;
}
minHeapNode* buildHuffmanTree(dataFreq* df, int size) {
    minHeapNode* left, * right, * top;
    minHeap* mh = createAndBuildMinHeap(df, size);
    while (mh->size > 1) {
        left = extractMin(mh);
        right = extractMin(mh);
        dataFreq* ndf = new dataFreq;
        ndf->data = '$'; // $ специальное значение для внутренних узлов
        ndf->freq = left->df->freq + right->df->freq;
        top = newNode(ndf);
        top->left = left;
        top->right = right;
        insertMinHeap(mh, top);
    }
    return extractMin(mh);
}
void printCodes(minHeapNode* root, int* arr, int top, codeTable ct[], int& ctSize) {
    if (root->left) {
        arr[top] = 0;
        printCodes(root->left, arr, top + 1, ct, ctSize);
    }
    if (root->right) {
        arr[top] = 1;
        printCodes(root->right, arr, top + 1, ct, ctSize);
    }
    if (!(root->left) && !(root->right)) { // если это листовой узел
        ct[ctSize].data = root->df->data;
        cout << root->df->data << ": ";
        for (int i = 0; i < top; i++) {
            ct[ctSize].code += to_string(arr[i]);
            cout << arr[i];
        }
        cout << "\n";
        ctSize++;
    }
}
void huffmanCodes(minHeapNode* root, codeTable ct[], int& ctSize) {
    int arr[256], top = 0;
    printCodes(root, arr, top, ct, ctSize);
}
string encodeDataHuffman(string str, codeTable ct[], int ct_size,ofstream &f) {
    string encoded = "";
    for (int i = 0; str[i] != '\0'; i++) {
        for (int j = 0; j < ct_size; j++) {
            if (str[i] == ct[j].data) {
                encoded += ct[j].code;
                break;
            }
        }
    }
    f << stoi(encoded) << endl;
    return encoded;
}
void decodeDataHuffman(minHeapNode* root, string& str) {
    string ans = "";
    minHeapNode* current = root;
    for (size_t i = 0; i < str.size(); i++) {
        if (str[i] == '0') {
            current = current->left;
        }
        else {
            current = current->right;
        }
        if (current->left == nullptr && current->right == nullptr) {
            ans += current->df->data;
            current = root;
        }
    }
    //cout << ans << "\n";
}
int main(int argc,char **argv) {
    if (argc < 2) { cout << "Usage: " << argv[0] << " filename"; return -1; }
    ifstream f(argv[1]);
    string buf, str;
    while (getline(f, buf)) {
        if (!str.empty()) {
            str += "\n";
        }
        str += buf;
    }
    f.close();
    dataFreq freqTable[256]; // в ASCII 256 символов
    int count = 0;
    for (int i = 0; str[i] != '\0'; i++) {
        bool found = false;
        for (int j = 0; j < count; j++) {
            if (str[i] == freqTable[j].data) {
                freqTable[j].freq++;
                found = true;
                break;
            }
        }
        if (!found) {
            freqTable[count].data = str[i];
            freqTable[count].freq = 1;
            count++;
        }
    }
    for (int i = 0; i < count; i++) {
        cout << freqTable[i].data << " " << freqTable[i].freq << '\n';
    }
    minHeapNode* root = buildHuffmanTree(freqTable, count);
    codeTable ct[256]; int ctSize = 0;
    huffmanCodes(root, ct, ctSize);
    ofstream f2("encoded_"+string(argv[1]), ios::binary);
    string encodedStr = encodeDataHuffman(str, ct, ctSize,f2);
    f2.close();
    decodeDataHuffman(root, encodedStr);
    return 0;
}