• Giga@hdv-tech.com
  • 24H Inkonzo ye-Intanethi:
    • 7189078c
    • sns03
    • 6660e33e
    • youtube 拷贝
    • instagram

    C, ukuFunda nokuBhala koXwebhu

    Ixesha lokuposa: Aug-11-2023

    Ichaza indlela umdwelisi wenkqubo we-C adala ngayo, avule, kwaye avale ifayile yokubhaliweyo, okanye ifayile yokubini.

    Ifayile, ithetha uthotho lwee-bytes, nokuba yifayile yokubhaliweyo okanye ifayile yokubini, C Language, ayinikezeli kuphela ukufikelela kwinqanaba eliphezulu lemisebenzi, kodwa ikwabonelela ngesiseko (OS) umnxeba wokucubungula iifayile kwisixhobo sokugcina. . Esi sahluko siya kuchaza iminxeba ebalulekileyo kulawulo lwamaxwebhu.

    Vula ifayile

    Ngesiqhelo usebenzisa i fopen () umsebenzi ukwenza ifayile entsha okanye ukuvula ifayile esele ikhona, le fowuni iqalisa into yodidi lwe FILE equlethe lonke ulwazi oluyimfuneko ukulawula ukuhamba. Nantsi iprototype yalo msebenzi wefowuni:

    FILE *fopen ( const char * igama lefayile , const char * indlela );

    Apha igama lefayile luluhlu lokubiza ifayile, ixabiso lendlela yofikelelo inokuba lelinye lamaxabiso alandelayo:

    ipateni

    inkcazelo

    r

    Vula ifayile yokubhaliweyo ekhoyo evumela ukuba ifundwe.

    w

    Vula ifayile yokubhaliweyo evumela ukubhala kwifayile. Ukuba ifayile ayikho, ifayile entsha yenziwe. Apha, inkqubo yakho ibhala umxholo ukusuka ekuqaleni kwefayile. Ukuba ifayile ikhona, iya kucuthwa ibe nguziro ubude kwaye iphinde ibhalwe.

    a

    Vula ifayile yombhalo kwaye ubhale kwifayile kwimodi yokudibanisa. Ukuba ifayile ayikho, ifayile entsha yenziwe. Apha, inkqubo yakho idibanisa umxholo kwiifayile osele unazo.

    r+

    Vula ifayile yokubhaliweyo ekuvumela ukuba ufunde kwaye ubhale ifayile.

    w+

    Vula ifayile yokubhaliweyo ekuvumela ukuba ufunde kwaye ubhale ifayile. Ukuba ifayile sele ikhona, ifayile inqunyulwa ibe ngu-zero ubude, kwaye ukuba ifayile ayikho, ifayile entsha yenziwe.

    a+

    Vula ifayile yokubhaliweyo ekuvumela ukuba ufunde kwaye ubhale ifayile. Ukuba ifayile ayikho, ifayile entsha yenziwe. Ukufundwa kuqala ekuqaleni kwefayile, kwaye ukubhala kukwimowudi yesihlomelo.

    Ukuba kuqwalaselwe ifayile yokubini, sebenzisa le ndlela yofikelelo ilandelayo ukubuyisela okungasentla:

    "rb", "wb", "ab", "rb+", "r+b", "wb+", "w+b", "ab+", "a+b"

    ifayile evaliweyo

    Ukuvala ifayile, nceda usebenzise i fclose() umsebenzi. Iprototype yomsebenzi imi ngolu hlobo lulandelayo:

    int fclose ( FILE * fp );

    • Ukuba ifayile ivalwe ngempumelelo, umsebenzi wefclose () ubuyisela u-zero, kwaye ukuba impazamo ibuyisela i-EOF. Lo msebenzi, enyanisweni, ususa idatha kwi-buffer, ivala ifayile, kwaye ikhuphe yonke imemori esetyenziselwa loo fayile. I-EOF isoloko ichazwa kwi-header file stdio.h

    Ilayibrari eqhelekileyo ye-C ibonelela ngemisebenzi eyahlukeneyo yokufunda nokubhala iifayile ngabalinganiswa okanye njengomtya wobude obusisigxina.

    Bhala kwifayile

    Nantsi eyona misebenzi ilula yokubhala abalinganiswa kumjelo:

    int fputc ( int c , FILE *fp );

    Umsebenzi fputc () ubhala ixabiso lomlinganiswa we parameter c kumjelo wemveliso ekhomba kuwo ifp. Ukuba ukubhala kuphumelele, ibuyisela uphawu olubhaliweyo kunye ne-EOF ukuba kwenzeka impazamo. Ungasebenzisa lo msebenzi ulandelayo ukubhala umtya ophela nge-null kumsinga:

    int fputs ( const char *s , FILE *fp );

    Umsebenzi fputs () ubhala umtya s kumjelo wemveliso apho ifp ikhomba khona. Ukuba ukubhala kuphumelele, ibuyisela ixabiso elingeyo-negative kunye ne-EOF ukuba kukho impazamo. Ungasebenzisa kwakhona umsebenzi we int fprintf (IFILE * fp, const char * ifomathi,...) ibhala umtya kwifayile. Zama lo mzekelo ulandelayo:

    Qaphela: Qiniseka ukuba unoluhlu lwe-tmp olukhoyo, kwaye ukuba alukho, kufuneka ulwenze kwikhompyuter yakho kuqala.

    / tmp idla ngokuba lulawulo lwexeshana kwindlela yeLinux. Ukuba usebenzisa inkqubo yeWindows, kufuneka uyiguqule kulawulo olukhoyo kwindawo yendawo, efana ne: C: \ tmp, D: \ tmp, njl.

    umzekelo ophilayo

    #zibandakanya int main () {IFILE *fp = NULL; fp = fopen ("/tmp/test.txt ", "w+"); fprintf (fp , "Olu luvavanyo lwefprintf...\n "); fputs ("Oku kuvavanyelwa iifputs...\n ", fp ); vala (fp); }

    Xa ikhowudi engentla idityanisiwe kwaye iphunyeziwe, yenza ifayile entsha test.txt kwi/tmp directory. Kwaye ubhalela imigca emibini esebenzisa imisebenzi emibini eyahlukeneyo. Masifunde le fayile ngokulandelayo.

    Funda ifayile

    Okulandelayo ngowona msebenzi ulula wokufunda unobumba omnye kwifayile:

    int fgetc ( FILE * fp );

    fgetc () umsebenzi ufunda umsebenzi osuka kwifayile yegalelo apho ifp ikhomba khona. Ixabiso lokubuyisela luphawu olufundiweyo kunye ne-EOF ukuba kukho impazamo. Umsebenzi olandelayo ikuvumela ukuba ufunde umtya kumsinga:

    char *fgets ( char *buf , int n , FILE *fp );

    Umsebenzi fgets () ufunda n-1 abasebenzi ukusuka kumjelo wegalelo elathiswe ngu fp. Ikhuphela umtya ofundiweyo kwibuffer buf kwaye ifakela unobumba ongenanto ekupheleni ukuphelisa umtya.

    Ukuba lo msebenzi udibana nomsebenzi womgca owaphukileyo '\ n' okanye i-EOF yesiphelo sefayile phambi kokufunda umsebenzi wokugqibela, emva koko ibuyiselwe kuphela kumagama afundiweyo, ukuquka ikhefu lomgca. Ungasebenzisa kwakhona i-int fscanf (FILE * fp, const char * format,...) umsebenzi wokufunda umtya kwifayile, kodwa iyeke ukufunda xa idibana nesithuba sokuqala kunye nekhefu lomgca.

    umzekelo ophilayo

    #zibandakanya int main () {IFILE *fp = NULL; char buff [255]; fp = fopen ("/tmp/test.txt ", "r"); fscanf (fp , "%s ", buff); printf ("1: %s \n ", buff ); fgets (buff, 255, (IFILE *)fp); printf ("2: %s \n ", buff ); fgets (buff, 255, (IFILE *)fp); printf ("3: %s \n ", buff ); vala (fp); }

    Xa ikhowudi engentla idityanisiwe kwaye iphunyeziwe, ifunda iifayile ezenziwe kwicandelo langaphambili, ivelisa ezi ziphumo zilandelayo:

    1: Le 2: ivavanya i-fprintf...

    3: Olu luvavanyo lweefputs...

    Kuqala, indlela ye fscanf () ifunda kuphela Oku .kuba idibana nesithuba ngasemva. Okwesibini, biza i-functon fgets () ukufunda inxalenye eseleyo kude kube sekupheleni komgca. Ekugqibeleni, fowunela fgets () ukufunda umqolo wesibini ngokupheleleyo.

    Umsebenzi weBinary I / O

    Imisebenzi emibini elandelayo isetyenziselwa igalelo lokubini kunye nemveliso:

    size_t fread (ingabikho *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 *ifayile );

    Yomibini imisebenzi ifundeka kwaye ibhale kwiibhloko zokugcina-ngokuqhelekileyo i-arrays okanye izakhiwo.

    Ngaphezulu malunga nefayile ye-C yokufunda nokubhala yeye-HDV Phoelectron Technology Ltd., umsebenzi wobugcisa besoftware. Kwaye inkampani yezixhobo ezinxulumene nenethiwekhi (ezifana ne: ACI-ONU/ unxibelelwanoI-ONU/ okrelekreleI-ONU/ ifayibhaI-ONU, njl.



    web聊天