Created
March 7, 2016 18:09
-
-
Save duythinht/8614c85a8afe1cb132bc to your computer and use it in GitHub Desktop.
Write integer as bytes
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 <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