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 32 33 34 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <stdint.h>
int main(int argc, char* argv[]) {
// read in 16 unsigned bit integers
// could use short, but on some platform, short may not be 16 bit
char const* filename = __FILE__ ".bin";
FILE* file = fopen(filename, "w");
uint16_t numbers[] = { 1, 2, 3, 4, 5 };
int length = sizeof(numbers) / sizeof(*numbers);
fwrite(numbers, sizeof(*numbers), length, file);
fclose(file);
// reading
file = fopen(filename, "r");
uint16_t array[5];
fread(array, sizeof(array[0]), 5, file);
for (int i = 0; i < 5; i++) {
printf("%u\n", array[i]);
}
fclose(file);
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2022 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.