Skip to content

Instantly share code, notes, and snippets.

@duythinht
Created March 7, 2016 18:09
Show Gist options
  • Save duythinht/8614c85a8afe1cb132bc to your computer and use it in GitHub Desktop.
Save duythinht/8614c85a8afe1cb132bc to your computer and use it in GitHub Desktop.
Write integer as bytes
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
int n = 1000000;
int i = 0;
FILE *f;
f = fopen("hello.txt", "w");
while (i<n) {
fwrite(&i, 1, sizeof(i), f);
++i;
}
fclose(f);
f = fopen("hello.txt", "r");
char buffer[4];
while (!feof(f)) {
fread(buffer, 1, sizeof(buffer), f);
int x = *(int*) buffer;
printf("%d\n", x);
}
fclose(f);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment