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

    C, Ukufunda Nokubhala Amadokhumenti

    Isikhathi sokuthumela: Aug-11-2023

    Ichaza ukuthi umhleli we-C udala, alivule futhi alivale kanjani ifayela lombhalo, noma ifayela elinambambili.

    Ifayela, lisho uchungechunge lwamabhayithi, kungakhathaliseki ukuthi ifayela lombhalo noma ifayela kanambambili, C Ulimi, alihlinzeki nje ngokufinyelela kumisebenzi yezinga eliphezulu, kodwa futhi linikeza ucingo oluyisisekelo (OS) lokucubungula amafayela kudivayisi yokugcina. . Lesi sahluko sizochaza izingcingo ezibalulekile ekuphathweni kwemibhalo.

    vula ifayela

    Ngokuvamile kusetshenziswa umsebenzi we-fopen () ukuze udale ifayela elisha noma uvule ifayela elikhona kakade, le kholi iqalisa into yohlobo lwe-FILE equkethe lonke ulwazi oludingekayo ukuze ulawule ukugeleza. Nasi isibonelo salolu hlelo lokusebenza:

    FILE *fopen ( const char * filename , const char * mode );

    Lapha igama lefayela liwuchungechunge lokuqamba ifayela, inani lemodi yokufinyelela lingaba elinye lamanani alandelayo:

    iphethini

    incazelo

    r

    Vula ifayela lombhalo elikhona elivumela ukuthi lifundwe.

    w

    Vula ifayela lombhalo elivumela ukubhala kufayela. Uma ifayela lingekho, ifayela elisha liyakhiwa. Lapha, uhlelo lwakho lubhala okuqukethwe kusukela ekuqaleni kwefayela. Uma ifayela likhona, lizoncishiswa libe nguziro ubude futhi libhalwe kabusha.

    a

    Vula ifayela lombhalo bese ubhalela ifayela kumodi ye-append. Uma ifayela lingekho, ifayela elisha liyakhiwa. Lapha, uhlelo lwakho lwengeza okuqukethwe kumafayela osuvele unawo.

    r+

    Vula ifayela lombhalo elikuvumela ukuthi ufunde futhi ubhale ifayela.

    w+

    Vula ifayela lombhalo elikuvumela ukuthi ufunde futhi ubhale ifayela. Uma ifayela selivele likhona, ifayela liyancishiswa libe nguziro ubude, futhi uma ifayela lingekho, kwakhiwa ifayela elisha.

    a+

    Vula ifayela lombhalo elikuvumela ukuthi ufunde futhi ubhale ifayela. Uma ifayela lingekho, ifayela elisha liyakhiwa. Ukufundwa kuqala ekuqaleni kwefayela, futhi ukubhala kumodi yokufaka kuphela.

    Uma licutshungulwe ifayela kanambambili, sebenzisa imodi yokufinyelela elandelayo ukuze umiselele okungenhla:

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

    ifayela elivaliwe

    Ukuvala ifayela, sicela usebenzise umsebenzi we-fclose(). I-prototype yomsebenzi imi kanje:

    int fclose ( FILE * fp );

    • Uma ifayela livaliwe ngempumelelo, umsebenzi we-fclose() ubuyisela uziro, futhi uma iphutha libuyisela i-EOF. Lo msebenzi, empeleni, ususa idatha ku-buffer, uvala ifayela, futhi ukhiphe yonke inkumbulo esetshenziselwa lelo fayela. I-EOF ichazwa njalo kunhlokweni yefayela elithi stdio.h

    Umtapo wezincwadi ojwayelekile we-C uhlinzeka ngemisebenzi ehlukahlukene yokufunda nokubhala amafayela ngezinhlamvu noma njengeyunithi yezinhlamvu yobude obugxilile.

    Bhala efayeleni

    Nansi imisebenzi elula yokubhala izinhlamvu ekusakazweni:

    int fputc (int c, FILE *fp);

    Umsebenzi fputc () ubhala inani lohlamvu lwepharamitha c emfudlaneni okukhiphayo i-fp ekhomba kukho. Uma ukubhala kuphumelele, kubuyisela uhlamvu olubhaliwe kanye ne-EOF uma kwenzeka iphutha. Ungasebenzisa umsebenzi olandelayo ukuze ubhale iyunithi yezinhlamvu egcina ngokungenalutho ekusakazweni:

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

    Umsebenzi othi fputs () ubhala iyunithi yezinhlamvu s emfudlaneni ophumayo lapho i-fp ikhomba khona. Uma ukubhala kuphumelela, kubuyisela inani elingelona inegethivu kanye ne-EOF uma kwenzeka iphutha. Ungasebenzisa futhi umsebenzi we-int fprintf (FILE * fp, const char * format,...) ibhala iyunithi yezinhlamvu efayeleni. Zama isibonelo esilandelayo:

    Qaphela:Qiniseka ukuthi unohla lwemibhalo olutholakalayo lwe-tmp, futhi uma lungekho, udinga ukulwenza kukhompuyutha yakho kuqala.

    / tmp ngokuvamile uhla lwemibhalo lwesikhashana ohlelweni lwe-Linux. Uma usebenzisa uhlelo lwe-Windows, udinga ukuluguqulela kuhla lwemibhalo olukhona endaweni yendawo, njenge: C: \ tmp, D: \ tmp, njll.

    isibonelo esiphilayo

    #faka int main () {IFILE *fp = NULL; fp = fopen ("/tmp/test.txt ", "w+"); fprintf (fp , "Lokhu ukuhlola i-fprintf...\n "); fputs ("Lokhu ukuhlola ama-fputs...\n ", fp ); ukuvala (fp); }

    Uma ikhodi engenhla ihlanganiswa futhi isetshenziswa, idala ifayela elisha elithi test.txt inthe/tmp directory. Futhi ubhalela imigqa emibili esebenzisa imisebenzi emibili ehlukene. Asifunde leli fayela ngokulandelayo.

    Funda ifayela

    Okulandelayo umsebenzi olula kakhulu wokufunda uhlamvu olulodwa efayelini:

    int fgetc (IFILE * fp);

    Umsebenzi we-fgetc () ufunda uhlamvu efayelini lokufaka lapho i-fp ikhomba khona. Inani lokubuyisela uhlamvu olufundiwe kanye ne-EOF uma kwenzeka iphutha. Umsebenzi olandelayo ukuvumela ukuthi ufunde iyunithi yezinhlamvu ephuma emfudlaneni:

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

    I-function fgets () ifunda izinhlamvu ze-n-1 kusukela ekusakazeni kokufaka okuqondiswa yi-fp. Ikopisha iyunithi yezinhlamvu efundiwe kubhafa bese yengeza uhlamvu olungenalutho ekugcineni ukuze inqamule uchungechunge.

    Uma lo msebenzi uhlangabezana nohlamvu lomugqa ophukile '\ n' noma i-EOF yokuphela kwefayela ngaphambi kokufunda uhlamvu lokugcina, bese ubuyiselwa kuphela ezinhlamvu ezifundiwe, okuhlanganisa ukuhlukana kwemigqa. Ungasebenzisa futhi umsebenzi we-int fscanf (FILE * fp, const char * format,...) ukuze ufunde iyunithi yezinhlamvu efayelini, kodwa iyeke ukufunda lapho ihlangabezana nesikhala sokuqala nokugqashuka komugqa.

    isibonelo esiphilayo

    #faka int main () {IFILE *fp = NULL; i-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 ); ukuvala (fp); }

    Lapho ikhodi engenhla ihlanganiswa futhi isetshenziswa, ifunda amafayela adalwe esigabeni esedlule, ikhiqize imiphumela elandelayo:

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

    3: Lokhu ukuhlola ama-fputs...

    Okokuqala, indlela ye-fscanf() ifunda kuphela Lokhu .ngoba ihlangabezana nesikhala ngemuva. Okwesibili, shayela i-functon fgets () ukuze ufunde ingxenye esele kuze kube sekupheleni komugqa. Ekugcineni, shayela ama-fgets () ukuze ufunde umugqa wesibili ngokuphelele.

    Kanambambili I / O umsebenzi

    Imisebenzi emibili elandelayo isetshenziselwa okokufaka nokukhiphayo kanambambili:

    size_t fread (okungenalutho *ptr , size_t size_of_elements, size_t number_of_elements, FILE *ifayela); size_t fwrite (const void *ptr , size_t size_of_elements , size_t number_of_elements , FILE *a_file );

    Yomibili imisebenzi iyafundwa futhi ibhalelwe amabhulokhi esitoreji-imvamisa ama-arrays noma izakhiwo.

    Ngaphezulu mayelana nokufundwa kwefayela nokubhala kwe-C kungokwe-HDV Phoelectron Technology Ltd., umsebenzi wobuchwepheshe wesofthiwe. Nenkampani yemishini ehlobene nenethiwekhi (njenge: ACI-ONU/ ukuxhumanaI-ONU/ ehlakaniphileI-ONU/ fiberI-ONU, njll.) ihlanganise ithimba lesofthiwe elinamandla, kuwo wonke amakhasimende enza ngendlela oyifisayo izimfuno ezikhethekile eziyidingayo, futhi ivumele imikhiqizo yethu ihlakaniphe futhi ithuthuke kakhulu.



    web聊天