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
27
28
29
30
31
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <stdint.h>  // gives us access to uint8_t

// Write one byte to a file.

// OK to copy/adapt this example.

int main(int argc, char* argv[]) {
    //uint8_t byte = 'A';  // 0x41
    uint8_t byte = 0xab;
    FILE* stream = fopen("fwrite_one_byte.txt", "w");  // "w" for writing to the file

    //     ┌─address to read data from
    //     │
    //     │      ┌─size of one chunk
    //     │      │
    //     │      │             ┌─number of chunks to write
    //     │      │             │
    //     │      │             │  ┌─stream to write the chunk to
    fwrite(&byte, sizeof(byte), 1, stream);
    fclose(stream);
    /*
     * The function fwrite() writes nmemb elements of data, each  size  bytes  long,  to
     * the stream referred to by stream, obtaining them from the location given by addr.
     */
    return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

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