1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
#include <stdio.h>
#include <stdlib.h>

#define COFFEE_BRAND_NUM_BYTES 6

#pragma pack(push)  // save the original data alignment
#pragma pack(1)     // Set data alignment to 1 byte boundary
typedef struct {
    char brand[COFFEE_BRAND_NUM_BYTES];
    char grade;
    int  num_votes;
} Coffee;
#pragma pack(pop) // restore the previous pack setting

int main(int argc, char* argv[]) {
    Coffee c1 = { .brand = "ABC01", .grade = 'A', .num_votes = 17 };
    FILE* fp = fopen("coffee_brand.bin", "w");
    fwrite(&c1, sizeof(c1), 1, fp);
    /*
     * size_t fwrite(const void *ptr, size_t size, size_t nmemb,
     *               FILE *stream);
     */
    fclose(fp);
    return EXIT_SUCCESS;
}

This content is protected and may not be shared, uploaded, or distributed.    © 2019 Alexander J. Quinn.