Skip to content

Instantly share code, notes, and snippets.

@mixstef
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mixstef/88b80ba4a2210ba2046b to your computer and use it in GitHub Desktop.
Save mixstef/88b80ba4a2210ba2046b to your computer and use it in GitHub Desktop.
Δυαδικό αρχείο vs αρχείο κειμένου

Εγγραφή τριών ακεραίων (125, 34, -7) σε αρχείο:

  • ως δυαδικό αρχείο
  • ως αρχείο κειμένου

Σε σχόλιο το αποτέλεσμα (hexdump).

#include <stdio.h>
#include <stdlib.h>
#define INUM 3
int main() {
FILE *fp;
int mat[] = {125,34,-7};
fp = fopen("test.bin","wb");
fwrite(mat,sizeof(int),INUM,fp);
fclose(fp);
return 0;
}
/* Hex dump:
$ hd test.bin
00000000 7d 00 00 00 22 00 00 00 f9 ff ff ff |}...".......|
0000000c
*/
#include <stdio.h>
#include <stdlib.h>
#define INUM 3
int main() {
FILE *fp;
int i;
int mat[] = {125,34,-7};
fp = fopen("test.txt","w");
for (i=0;i<INUM;i++) {
fprintf(fp,"%d\n",mat[i]);
}
fclose(fp);
return 0;
}
/* Hex dump:
$ hd test.txt
00000000 31 32 35 0a 33 34 0a 2d 37 0a |125.34.-7.|
0000000a
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment