1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #include <stdio.h> #include <stdlib.h> typedef struct { int age; char name[ 10 ]; int weight; }Person; int main() { Person person = { 20, "Jane", 50 }; // 存資料進檔案 FILE *file = fopen( "test.txt", "wb" ); fwrite( &person, sizeof(Person), 1, file ); fclose( file ); // 資料清空 memset( &person, 0, sizeof(Person) ); // 讀資料回來 file = fopen( "test.txt", "rb" ); fread( &person, sizeof(Person), 1, file ); fclose( file ); printf( "%d,%s,%d\n", person.age, person.name, person.weight ); system("pause"); return EXIT_SUCCESS; } |
Direct link: https://paste.plurk.com/show/281215