/*****************************************************************
Name : C 檔案<->結構 讀寫
Date : 2017/06/17
By : CharlotteHonG
Final: 2017/06/17
*****************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int No;
char name [ 20 ] ;
char phoneNumber[ 30 ] ;
char Email_address[ 45 ] ;
} Contact;
// 新增一筆資料
void app_Person( char * filename, Contact* con) {
char * lf= "\n " ;
char * tok= "," ;
char temp[ 32 ] ;
FILE
* pFile
= fopen ( filename
, "ab+" ) ; fwrite ( con
-> phoneNumber
, sizeof ( char ) , strlen ( con
-> phoneNumber
) , pFile
) ; fwrite ( con
-> Email_address
, sizeof ( char ) , strlen ( con
-> Email_address
) , pFile
) ; }
// 寫入通訊錄
void write_Contacts( char * filename , Contact* con, size_t len) {
FILE
* pFile
= fopen ( filename
, "wb+" ) ; if ( ! pFile) {
}
for ( unsigned i = 0 ; i < len; ++ i) {
app_Person( filename, con+ i) ;
}
}
// 讀取通訊錄
void read_Contacts( char * filename , Contact* con, size_t len) {
FILE
* pFile
= fopen ( filename
, "rb" ) ; if ( ! pFile) {
}
for ( char buf
[ 128 ] , * pch
; fgets ( buf
, 128 , pFile
) != NULL
; ++ con
) { strcpy ( con
-> phoneNumber
, pch
) ; strcpy ( con
-> Email_address
, pch
) ; }
}
// 讀取通訊錄RAW
void read_ContactsRaw( char * filename, char * buf) {
FILE
* pFile
= fopen ( filename
, "rb" ) ; if ( ! pFile) {
}
fseek ( pFile
, 0 , SEEK_END
) ; long lSize
= ftell ( pFile
) ; fread ( buf
, 1 , lSize
, pFile
) ; }
// 印出結構
void pri_person( Contact* con) {
printf ( "%d, %s, %s, %s" , con
-> No
, con
-> name
, con
-> phoneNumber
, con
-> Email_address
) ; }
void pri_Contacts( Contact* con, size_t len) {
for ( unsigned i = 0 ; i < len; ++ i) {
pri_person( con+ i) ;
}
}
// 印出通訊錄
int main( void ) {
Contact w_con[ 2 ] = {
{ 1 , "AAA" , "0912345678" , "AAA@gmail.com" } ,
{ 2 , "BBB" , "0912345678" , "BBB@gmail.com" }
} ;
// 寫入通訊錄
write_Contacts( "Contact.bin" , w_con, 2 ) ;
// 讀取通訊錄
Contact r_con[ 2 ] ;
read_Contacts( "Contact.bin" , r_con, 2 ) ;
pri_Contacts( r_con, 2 ) ;
// 讀取檔案
char buf[ 256 ] ;
read_ContactsRaw( "Contact.bin" , buf) ;
}
LyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqCk5hbWUgOiBDIOaqlOahiDwtPue1kOaniyDoroDlr6sKRGF0ZSA6IDIwMTcvMDYvMTcKQnkgICA6IENoYXJsb3R0ZUhvbkcKRmluYWw6IDIwMTcvMDYvMTcKKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKiovCiNpbmNsdWRlIDxzdGRpby5oPgojaW5jbHVkZSA8c3RkbGliLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KCnR5cGVkZWYgc3RydWN0IHsKICAgIGludCBObzsKICAgIGNoYXIgbmFtZSBbMjBdOwogICAgY2hhciBwaG9uZU51bWJlclszMF07CiAgICBjaGFyIEVtYWlsX2FkZHJlc3NbNDVdOwp9IENvbnRhY3Q7Ci8vIOaWsOWinuS4gOethuizh+aWmQp2b2lkIGFwcF9QZXJzb24oY2hhciogZmlsZW5hbWUsIENvbnRhY3QqIGNvbikgewogICAgY2hhciogbGY9IlxuIjsKICAgIGNoYXIqIHRvaz0iLCI7CgogICAgY2hhciB0ZW1wWzMyXTsKICAgIGl0b2EoY29uLT5ObywgdGVtcCwgMTApOwoKICAgIEZJTEUgKnBGaWxlID0gZm9wZW4oZmlsZW5hbWUsImFiKyIpOwogICAgZndyaXRlKHRlbXAsIHNpemVvZihjaGFyKSwgc3RybGVuKHRlbXApLCBwRmlsZSk7CiAgICBmd3JpdGUodG9rLCBzaXplb2YoY2hhciksIHN0cmxlbih0b2spLCBwRmlsZSk7CiAgICBm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