Εγγραφή τριών ακεραίων (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 | |
*/ |