Untitled

mail@pastecode.io avatar
unknown
c_cpp
7 months ago
3.9 kB
4
Indexable
Never
// Your code looks well-structured and functional for its purpose. However, there are a few suggestions for improvement:

// Error Handling: Your error handling is good, but you might consider providing more informative error messages. For example, specifying the type of error (e.g., file not found, unable to open file) can help during debugging.

// Constants and Magic Values: Instead of using hard-coded file paths, consider using constants or command-line arguments. This makes the code more flexible and easier to reuse.

// Comments and Documentation: Consider adding more comments to explain the purpose of specific parts of your code, especially if it's intended for others to read or if you plan to revisit the code after some time. Documentation // can also include a brief overview of the program.

// Functions for File I/O: Consider breaking down the file I/O operations into separate functions. This can improve readability and make the main function more concise. For example, you could have functions for reading the file, // writing to CSV, and writing to TXT.

// Here's an example incorporating some of these suggestions:

#include <iostream>
#include <fstream>
#include <vector>
#include <cstdint>

using namespace std;

// Define the struct to represent the records
#pragma pack(push, 1)
struct Record {
    uint16_t A;
    char B;
    uint8_t : 8;
    uint32_t D;
    float E;
    uint64_t F;
};
#pragma pack(pop)

static_assert(sizeof(Record) == 20, "Size of Record struct must be 20 bytes.");

uint32_t swap_endian(uint32_t value) {
    return ((value & 0xFF000000) >> 24) |
           ((value & 0x00FF0000) >> 8) |
           ((value & 0x0000FF00) << 8) |
           ((value & 0x000000FF) << 24);
}

bool readBinaryFile(const char* file_path, vector<Record>& records) {
    ifstream file(file_path, ios::binary);

    if (!file.is_open()) {
        cerr << "Error opening file: " << file_path << endl;
        return false;
    }

    Record temp_record;
    while (file.read(reinterpret_cast<char*>(&temp_record), sizeof(Record))) {
        temp_record.D = swap_endian(temp_record.D);
        records.push_back(temp_record);
    }

    file.close();
    return true;
}

bool writeCSV(const char* csv_path, const vector<Record>& records) {
    ofstream output_csv(csv_path);

    if (!output_csv.is_open()) {
        cerr << "Error opening CSV file: " << csv_path << endl;
        return false;
    }

    // Write the header to the CSV file
    output_csv << "A,D,E,G" << endl;

    // Write each record to the CSV file
    for (const auto& record : records) {
        output_csv << record.A << "," << record.D << "," << record.E << "," << ((record.F >> 10) & 0x7F) << endl;
    }

    output_csv.close();
    return true;
}

bool writeTXT(const char* txt_path, const vector<Record>& records) {
    ofstream output_txt(txt_path);

    if (!output_txt.is_open()) {
        cerr << "Error opening TXT file: " << txt_path << endl;
        return false;
    }

    // Write each character to the TXT file
    for (const auto& record : records) {
        output_txt << record.B;
    }

    output_txt.close();
    return true;
}

int main() {
    const char* file_path = "sample_data.bin";
    const char* output_csv_path = "out.csv";
    const char* output_message_path = "message.txt";

    vector<Record> records;

    // Read binary file
    if (!readBinaryFile(file_path, records)) {
        return 1;
    }

    // Write to CSV
    if (!writeCSV(output_csv_path, records)) {
        return 2;
    }

    // Write to TXT
    if (!writeTXT(output_message_path, records)) {
        return 3;
    }

    cout << "Export complete." << endl;

    return 0;
}
// These changes aim to enhance readability and maintainability. Feel free to adapt them based on your preferences and specific requirements.
Leave a Comment