Created
April 18, 2016 14:24
-
-
Save sepi/05e3f3b4085fdf58ff3ccab8e96400fd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> /* fir fopen, fprintf an fread */ | |
#include <stddef.h> /* fir sizt_t */ | |
#include <stdint.h> /* fir uint8_t */ | |
#include <stdlib.h> /* fir calloc */ | |
#define PATH "binary-file.bin" | |
int | |
main(int argc, char *argv[]) { | |
FILE *f; | |
f = fopen(PATH, "rb"); /* rb steet fir "read" and "binary", | |
de modus an deems de d'datei wells | |
benotzen */ | |
/* Bessi error handling */ | |
if (f == NULL) { | |
fprintf(stderr, "Could not open file\n"); | |
return -1; | |
} | |
size_t octets_in_file = 100; /* Wanns de scho wees wei grouss deng | |
datei as */ | |
uint8_t *data; | |
data = (uint8_t *)calloc(sizeof(uint8_t), octets_in_file); | |
if (data == NULL) { | |
fprintf(stderr, "Could not allocate memory\n"); | |
return -1; | |
} | |
int octets_read = fread((void *)data, sizeof(uint8_t), octets_in_file, f); | |
printf("Read %d octets from file\n", octets_read); | |
/* Wat gelies gouf printen */ | |
for (int i; i < octets_read; ++i) { | |
printf("0x%02X ", data[i]); | |
if (i != 0 && i % 10 == 0) { | |
printf("\n"); | |
} | |
} | |
/* Exit status setzen falls gewenscht */ | |
if (octets_read == octets_in_file) { | |
return 0; | |
} else { | |
return -1; | |
} | |
} | |
// Ze compileiren z.B. mat: gcc -std=c99 -Wpedantic -Wall -g binaryread.c -o binaryread |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment