Piav qhia li cas C programmer tsim, qhib, thiab kaw cov ntawv nyeem, lossis cov ntaub ntawv binary.
Ib cov ntaub ntawv, txhais tau hais tias ib tug series ntawm bytes, txawm nws yog ib tug ntawv nyeem ntawv los yog ib tug binary ntaub ntawv, C Language, tsis tsuas yog muab kev nkag tau mus rau lub sab saum toj theem functions, tab sis kuj muab lub hauv paus (OS) hu rau cov txheej txheem cov ntaub ntawv ntawm lub cia ntaus ntawv. . Tshooj no yuav piav qhia txog kev hu xov tooj tseem ceeb hauv kev tswj cov ntaub ntawv.
qhib-file
Feem ntau siv fopen () ua haujlwm los tsim cov ntaub ntawv tshiab lossis qhib cov ntaub ntawv uas twb muaj lawm, qhov kev hu no pib ua ib qho khoom ntawm hom FILE uas muaj tag nrho cov ntaub ntawv tsim nyog los tswj cov ntws. Nov yog tus qauv ntawm qhov kev hu xov tooj no:
FILE *fopen ( const char * filename , const char * hom );
Ntawm no filename yog ib txoj hlua rau npe cov ntaub ntawv, tus nqi ntawm kev siv hom tuaj yeem yog ib qho ntawm cov txiaj ntsig hauv qab no:
qauv | piav qhia |
r | Qhib cov ntawv nyeem uas twb muaj lawm uas tso cai rau nws nyeem. |
w | Qhib cov ntawv nyeem uas tso cai rau sau rau cov ntaub ntawv. Yog tias tsis muaj cov ntaub ntawv, cov ntaub ntawv tshiab raug tsim. Ntawm no, koj qhov kev pab cuam sau cov ntsiab lus los ntawm qhov pib ntawm cov ntaub ntawv. Yog tias cov ntaub ntawv muaj nyob, nws yuav raug txiav mus rau xoom ntev thiab rov sau dua. |
a | Qhib cov ntawv nyeem thiab sau rau hauv cov ntaub ntawv hauv ib qho append hom. Yog tias tsis muaj cov ntaub ntawv, cov ntaub ntawv tshiab raug tsim. Ntawm no, koj qhov kev pab cuam ntxiv cov ntsiab lus rau cov ntaub ntawv koj twb muaj lawm. |
r+ | Qhib cov ntawv nyeem uas tso cai rau koj nyeem thiab sau cov ntaub ntawv. |
w+ | Qhib cov ntawv nyeem uas tso cai rau koj nyeem thiab sau cov ntaub ntawv. Yog tias cov ntaub ntawv twb muaj lawm, cov ntaub ntawv raug txiav mus rau xoom ntev, thiab yog tias cov ntaub ntawv tsis muaj nyob, cov ntaub ntawv tshiab raug tsim. |
a+ | Qhib cov ntawv nyeem uas tso cai rau koj nyeem thiab sau cov ntaub ntawv. Yog tias tsis muaj cov ntaub ntawv, cov ntaub ntawv tshiab raug tsim. Kev nyeem pib thaum pib ntawm cov ntaub ntawv, thiab kev sau tsuas yog nyob rau hauv append hom. |
Yog tias ua tiav cov ntaub ntawv binary, siv hom kev nkag hauv qab no los hloov cov saum toj no:
"rb", "wb", "ab", "rb+", "r+b", "wb+", "w+b", "ab+", "a+b"
kaw cov ntaub ntawv
Txhawm rau kaw cov ntaub ntawv, thov siv fclose() ua haujlwm. Tus qauv ntawm kev ua haujlwm yog raws li hauv qab no:
int fclose ( FILE * fp );
- Yog tias cov ntaub ntawv raug kaw tiav, fclose() muaj nuj nqi rov xoom, thiab yog tias qhov yuam kev rov qab EOF. Qhov kev ua haujlwm no, qhov tseeb, tshem tawm cov ntaub ntawv los ntawm qhov tsis, kaw cov ntaub ntawv, thiab tso tawm tag nrho cov cim xeeb siv rau cov ntaub ntawv ntawd. EOF yog ib qho tas mus li txhais hauv cov ntaub ntawv header stdio.h
Lub tsev qiv ntawv C tus qauv muab ntau yam haujlwm los nyeem thiab sau cov ntaub ntawv los ntawm cov cim lossis ua ib txoj hlua ntev ntev.
Sau rau hauv cov ntaub ntawv
Nov yog cov haujlwm yooj yim tshaj plaws los sau cov cim rau hauv kwj:
int fputc ( int c , FILE *fp );
Qhov kev ua haujlwm fputc () sau tus cwj pwm tus nqi ntawm qhov ntsuas c rau hauv cov zis tawm uas fp taw qhia rau. Yog tias cov ntawv sau ua tiav, nws rov qab sau cov cim thiab EOF yog tias qhov yuam kev tshwm sim. Koj tuaj yeem siv cov haujlwm hauv qab no los sau ib txoj hlua xaus nrog null rau cov kwj deg:
int fputs ( const char *s , FILE * fp );
Qhov kev ua haujlwm fputs () sau cov hlua s mus rau qhov tso zis tawm qhov twg fp taw qhia rau. Yog tias cov ntawv sau ua tiav, nws xa rov qab tus nqi tsis zoo thiab EOF yog qhov yuam kev tshwm sim. Koj tuaj yeem siv cov haujlwm ntawm int fprintf (FILE * fp, const char * hom, ...) sau ib txoj hlua rau cov ntaub ntawv. Sim ua piv txwv hauv qab no:
Nco tseg: Nco ntsoov tias koj muaj tmp directory, thiab yog tias nws tsis muaj, koj yuav tsum tsim nws hauv koj lub computer ua ntej.
/ tmp feem ntau yog cov npe ib ntus ntawm Linux system. Yog tias koj khiav ntawm lub Windows system, koj yuav tsum hloov kho nws mus rau cov npe muaj nyob hauv ib puag ncig hauv zos, xws li: C: \ tmp, D: \ tmp, thiab lwm yam.
ua piv txwv
# suav nrog
Thaum cov cai saum toj no tau muab tso ua ke thiab ua tiav, nws tsim cov ntaub ntawv tshiab test.txt hauv / tmp directory. Thiab sau rau ob kab uas siv ob txoj haujlwm sib txawv. Cia peb nyeem cov ntawv no tom ntej.
Nyeem cov ntaub ntawv
Cov hauv qab no yog qhov ua haujlwm yooj yim tshaj plaws los nyeem ib tus cim los ntawm cov ntaub ntawv:
int fgetc ( FILE * fp );
Lub fgetc () muaj nuj nqi nyeem ib tug cim los ntawm cov ntaub ntawv input uas cov ntsiab lus fp. Tus nqi xa rov qab yog tus nyeem cim thiab EOF yog tias muaj teeb meem tshwm sim. Cov haujlwm hauv qab no tso cai rau koj nyeem ib txoj hlua los ntawm kwj:
char *fgets ( char *buf , int n , FILE *fp );
Qhov kev ua haujlwm fgets () nyeem n-1 cim los ntawm cov kwj input qhia los ntawm fp. Nws theej cov kab nyeem ntawv mus rau qhov tsis buf thiab ntxiv ib tus cim null ntawm qhov kawg kom xaus txoj hlua.
Yog tias txoj haujlwm no ntsib cov kab cim tawg '\n' lossis EOF ntawm qhov kawg ntawm cov ntaub ntawv ua ntej nyeem cov cim kawg, tsuas yog rov qab mus rau cov cim nyeem, suav nrog cov kab tawg. Koj tuaj yeem siv int fscanf (FILE * fp, const char * format, ...) ua haujlwm los nyeem cov hlua ntawm cov ntaub ntawv, tab sis nws nres nyeem ntawv thaum ntsib thawj qhov chaw thiab kab tawg.
ua piv txwv
# suav nrog
Thaum cov cai saum toj no tau muab tso ua ke thiab ua tiav, nws nyeem cov ntaub ntawv tsim hauv ntu dhau los, ua cov txiaj ntsig hauv qab no:
1: Qhov no 2: yog kev sim rau fprintf...
3: Qhov no yog kev sim rau fputs ...
Ua ntej, txoj kev fscanf() tsuas yog nyeem Qhov no .vim nws ntsib qhov chaw nyob tom qab. Thib ob, hu rau functon fgets () kom nyeem cov seem seem kom txog thaum kawg ntawm kab. Thaum kawg, hu rau fgets () nyeem kab thib ob kom tiav.
Binary I / O muaj nuj nqi
Ob txoj haujlwm hauv qab no yog siv rau binary input thiab output:
size_t fread (void *ptr , size_t size_of_elements , size_t number_of_elements , FILE *a_file ); size_t fwrite (const void *ptr , size_t size_of_elements , size_t number_of_elements , FILE *a_file );
Ob qho kev ua haujlwm tau nyeem thiab sau cia rau cov blocks-feem ntau yog cov arrays lossis cov qauv.
Saum toj no hais txog C cov ntaub ntawv nyeem thiab sau ntawv belongs rau HDV Phoelectron Technology Ltd., software kev lag luam. Thiab lub tuam txhab rau cov khoom siv network (xws li: ACONU/ kev sib txuas lusONU/ ntseONU/ fiber ntauONU.