#Custom DCT (Document) File Format - Fun Side Project

10 messages · Page 1 of 1 (latest)

tame fiber
#

i decided to create my own file, or file format; or however you wanna call it. its called DCT or Document. i didnt have a specific idea for what kind of file to make since i treated this as just a fun little side project. All you do is store text. its not efficient nor remotely useful for real world purposes, but as i said.... its just a fun side project.

personally i think the api for this is pretty clean, here a little demo:

int main()
{
    // demonstrate failure
    create_dct_file("fail1");
    create_dct_file("fail2.weird");
    
    // creates a valid dct (document) file
    create_dct_file("demo.dct");
  
    // read header out of file
    struct dct_header hd = read_dct_file_header("demo.dct");
    hd.dct[0] = 'x'; // manipulate & invalidate for next test case
    write_dct_file_header("demo.dct", &hd); // will fail because it validates header internally

    // log
    printf("\n"); // Seperator to Seperate from Earlier Functions Error Case Testings
    printf("sizeof(struct dct_header): %llu\n", sizeof(struct dct_header));
    printf("Old Character Count: %hu\n", hd.char_count);
    printf("Old File Size: %llu\n", hd.file_size);

    // write text into file
    char txt1[] = "Hello, DCT. ";
    char txt2[] = "DCT is the short version for \"Document\"";

    write_dct_text("demo.dct", txt1, sizeof(txt1) - 1);
    write_dct_text("demo.dct", txt2, sizeof(txt2) - 1);

    hd = read_dct_file_header("demo.dct"); // read updated header (write_dct_text updates char count and size internally)
    
    // log
    printf("New Character Count: %hu\n", hd.char_count);
    printf("New File Size: %llu\n\n", hd.file_size);

    print_file_bytes("demo.dct");

    return 0;
}
#

^ the image in the post is the output result of the demo ^

also here is how the header structure looks like:

struct dct_header {
    char dct[3]; // identifier
    long long file_size; // full size
    short version;
    uint64_t char_count;
    char spacing[32];
};
potent magnet
#

your header struct has lots of internal padding

foggy sluice
tame fiber
#

pretty minimal difference in bytes, but i assume if the amount of header structures start stacking the size will eventually matter

#

still nice thank you 🙏

potent magnet
#

what i was concerned about is whether or not the struct is just written into a file raw

tame fiber
#

it is, i just fwrite the struct straight into the file

potent magnet
#

a raw struct will have padding between variables, some of which might be uninitialized