Amafotokoza momwe C programmer amapangira, kutsegulira, ndi kutseka fayilo, kapena fayilo ya binary.
Fayilo, imatanthawuza mndandanda wa ma byte, kaya ndi fayilo yolemba kapena fayilo ya binary, C Language, sikuti imangopereka mwayi wopeza ntchito zapamwamba, komanso imaperekanso kuyimba kwapansi (OS) kukonza mafayilo pa chipangizo chosungira. . Mutuwu ufotokoza zofunikira pakuwongolera zolemba.
Open-file
Nthawi zambiri pogwiritsa ntchito fopen () ntchito kuti mupange fayilo yatsopano kapena kutsegula fayilo yomwe ilipo, kuyimba kumeneku kumayambitsa chinthu chamtundu wa FILE chomwe chili ndi zidziwitso zonse zofunika kuwongolera kuyenda. Nayi fanizo la ntchito iyi:
FILE * fopen ( const char * filename , const char * mode );
Apa filename ndi chingwe chotchulira fayilo, mtengo wanjira yofikira ukhoza kukhala chimodzi mwazinthu zotsatirazi:
chitsanzo | kufotokoza |
r | Tsegulani fayilo yomwe ilipo yomwe imalola kuti iwerengedwe. |
w | Tsegulani fayilo yomwe imalola kulemba ku fayilo. Ngati fayiloyo kulibe, fayilo yatsopano imapangidwa. Apa, pulogalamu yanu imalemba zomwe zili kuyambira pachiyambi cha fayilo. Ngati fayiloyo ilipo, idzadulidwa mpaka ziro kutalika ndikulembedwanso. |
a | Tsegulani fayilo ndikulembera fayiloyo munjira yowonjezera. Ngati fayiloyo kulibe, fayilo yatsopano imapangidwa. Apa, pulogalamu yanu imawonjezera zomwe zili m'mafayilo omwe muli nawo kale. |
r+ | Tsegulani fayilo yomwe imakupatsani mwayi wowerenga ndikulemba fayiloyo. |
w+ | Tsegulani fayilo yomwe imakupatsani mwayi wowerenga ndikulemba fayiloyo. Ngati fayiloyo ilipo kale, fayiloyo imadulidwa mpaka zero kutalika, ndipo ngati fayilo palibe, fayilo yatsopano imapangidwa. |
a+ | Tsegulani fayilo yomwe imakupatsani mwayi wowerenga ndikulemba fayiloyo. Ngati fayiloyo kulibe, fayilo yatsopano imapangidwa. Kuwerenga kumayambira kumayambiriro kwa fayilo, ndipo kulemba kumangowonjezera. |
Ngati fayilo ya binary yasinthidwa, gwiritsani ntchito njira zotsatirazi kuti musinthe zomwe zili pamwambapa:
"rb", "wb", "ab", "rb+", "r+b", "wb+", "w+b", "ab+", "a+b"
fayilo yotsekedwa
Kuti mutseke fayilo, chonde gwiritsani ntchito fclose() ntchito. The prototype ntchito ndi motere:
int fclose ( FILE * fp );
- Ngati fayilo yatsekedwa bwino, fclose () ntchito imabwezeretsa zero, ndipo ngati cholakwikacho chibwereranso EOF. Ntchitoyi, imachotsa deta kuchokera ku buffer, kutseka fayilo, ndikumasula kukumbukira zonse zomwe zimagwiritsidwa ntchito pa fayiloyo. EOF imatanthauzidwa nthawi zonse mu fayilo yamutu stdio.h
Laibulale yokhazikika ya C imapereka ntchito zosiyanasiyana kuti muwerenge ndi kulemba mafayilo ndi zilembo kapena ngati chingwe chokhazikika.
Lembani ku fayilo
Nazi ntchito zosavuta kulemba zilembo kumtsinje:
int fputc (int c, FILE *fp);
Ntchito fputc () imalemba mtengo wa parameter c mumtsinje womwe fp ikulozera. Ngati zolembazo zikuyenda bwino, zimabwezera munthu wolembedwa ndi EOF ngati cholakwika chichitika. Mutha kugwiritsa ntchito zotsatirazi kuti mulembe chingwe chomaliza ndi null kumtsinje:
int fputs ( const char *s , FILE *fp );
Ntchito fputs () imalemba chingwe s kumtsinje womwe fp imalozera. Ngati zolembazo zikuyenda bwino, zimabwezera mtengo wosakhala woipa ndi EOF ngati cholakwika chichitika. Mukhozanso kugwiritsa ntchito int fprintf (FILE * fp, const char * format,...) amalemba chingwe ku fayilo. Yesani chitsanzo ichi:
Zindikirani: Onetsetsani kuti muli ndi tmp directory, ndipo ngati palibe, muyenera kulenga pa kompyuta kaye.
/ tmp nthawi zambiri ndi chikwatu chakanthawi pa Linux system. Ngati muthamanga pa Windows system, muyenera kuyisintha kukhala chikwatu chomwe chilipo mdera lanu, monga: C: \ tmp, D: \ tmp, etc.
chitsanzo chamoyo
#kuphatikizapo
Khodi yomwe ili pamwambapa ikapangidwa ndikuchitidwa, imapanga fayilo yatsopano test.txt inthe/tmp directory. Ndipo amalembera mizere iwiri pogwiritsa ntchito ntchito ziwiri zosiyana. Tiyeni tiwerenge fayiloyi kenako.
Werengani fayilo
Zotsatirazi ndi ntchito yosavuta kuwerenga munthu m'modzi kuchokera pafayilo:
int fgetc (FILE * fp);
Ntchito ya fgetc () imawerengera zilembo kuchokera pafayilo yolowera komwe fp imalozera. Mtengo wobwerera ndi wowerengedwa ndi EOF ngati cholakwika chichitika. Ntchito yotsatirayi imakupatsani mwayi wowerenga chingwe kuchokera pamtsinje:
char *fgets ( char *buf , int n , FILE *fp );
Ntchito fgets () imawerenga zilembo za n-1 kuchokera pamakina olowera motsogozedwa ndi fp. Imakopera chingwe chowerengedwa ku buffer buf ndikuyika zilembo zachabechabe kumapeto kuti zithetse chingwecho.
Ngati ntchitoyi ikukumana ndi mzere wosweka '\ n' kapena EOF ya mapeto a fayilo musanawerenge khalidwe lomaliza, ndiye kuti amangobwerera ku malemba owerengedwa, kuphatikizapo kusweka kwa mzere. Mukhozanso kugwiritsa ntchito int fscanf (FILE * fp, const char * format,...) ntchito kuti muwerenge chingwe kuchokera pa fayilo, koma imasiya kuwerenga pamene ikukumana ndi malo oyamba ndi mzere wosweka.
chitsanzo chamoyo
#kuphatikizapo
Khodi yomwe ili pamwambapa ikapangidwa ndikuchitidwa, imawerenga mafayilo omwe adapangidwa m'gawo lapitalo, ndikupanga zotsatirazi:
1: Ichi 2: ndikuyesa fprintf...
3: Uku ndikuyesa ma fputs ...
Choyamba, njira ya fscanf () imangowerenga Izi .chifukwa imakumana ndi malo kumbuyo. Chachiwiri, imbani functon fgets () kuti muwerenge gawo lotsalira mpaka kumapeto kwa mzere. Pomaliza, imbani fgets () kuti muwerenge mzere wachiwiri kwathunthu.
Binary I / O ntchito
Ntchito ziwiri zotsatirazi zimagwiritsidwa ntchito pakulowetsa ndi kutulutsa kwa binary:
size_t fread (zopanda *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file); size_t fwrite (consst void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file);
Ntchito zonse ziwiri zimawerengedwa ndikulemba zosungirako - nthawi zambiri masanjidwe kapena mapangidwe.
Pamwambapa pafupifupi C file kuwerenga ndi kulemba ndi HDV Phoelectron Technology Ltd., mapulogalamu luso ntchito. Ndi kampani yopanga zida zokhudzana ndi maukonde (monga: ACONU/ kulumikizanaONU/ wanzeruONU/ fiberONU, etc.) wabweretsa pamodzi gulu lamphamvu mapulogalamu, kwa kasitomala aliyense makonda yekha amafuna, komanso kulola katundu wathu wanzeru ndi patsogolo.