• Giga@hdv-tech.com
  • 24H lawelawe pūnaewele:
    • 7189078c
    • sns03
    • 6660e33e
    • youtube 拷贝
    • instagram

    C, Heluhelu a kakau palapala

    Ka manawa hoʻouna: ʻAukake-11-2023

    Hōʻike i ke ʻano o ka hana ʻana, wehe a pani ʻana o ka mea papahana C i kahi faila kikokikona, a i ʻole kahi faila binary.

    ʻO kahi faila, ʻo ia hoʻi he ʻano o nā bytes, inā he faila kikokikona a i ʻole ka faila binary, C Language, ʻaʻole wale e hāʻawi i ke komo i nā hana kiʻekiʻe, akā hāʻawi pū kekahi i ke kelepona kumu (OS) e hoʻoponopono i nā faila ma ka mea mālama. . E wehewehe ana kēia mokuna i nā kāhea koʻikoʻi i ka mālama palapala.

    waihona wehe

    Hoʻohana maʻamau i ka hana fopen () no ka hana ʻana i kahi faila hou a wehe paha i kahi faila i loaʻa, hoʻomaka kēia kelepona i kahi mea o ke ʻano FILE i loaʻa nā ʻike pono āpau e hoʻokele i ke kahe. Eia ka prototype o kēia kelepona hana:

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

    Eia ka inoa file he string e kapa ai i kahi faila, ʻo ka waiwai o ke ala komo hiki ke lilo i kekahi o kēia mau waiwai:

    kumu hoʻohālike

    wehewehe

    r

    E wehe i kahi waihona kikokikona e hiki ai ke heluhelu ʻia.

    w

    E wehe i kahi waihona kikokikona e hiki ai ke kākau i ka faila. Inā ʻaʻole i loaʻa ka faila, hana ʻia kahi faila hou. Maʻaneʻi, kākau kāu polokalamu i ka ʻike mai ka hoʻomaka ʻana o ka faila. Inā loaʻa ka faila, e ʻoki ʻia i ka lōʻihi ʻole a kākau hou ʻia.

    a

    E wehe i kahi waihona kikokikona a kākau i ka faila ma ke ʻano hoʻohui. Inā ʻaʻole i loaʻa ka faila, hana ʻia kahi faila hou. Maʻaneʻi, hoʻopili kāu polokalamu i nā ʻike i nā faila āu i loaʻa ai.

    r+

    E wehe i kahi waihona kikokikona e hiki ai iā ʻoe ke heluhelu a kākau i ka faila.

    w+

    E wehe i kahi waihona kikokikona e hiki ai iā ʻoe ke heluhelu a kākau i ka faila. Inā loaʻa ka faila, ua ʻoki ʻia ka faila i ʻole ka lōʻihi, a inā ʻaʻole i loaʻa ka faila, hana ʻia kahi faila hou.

    a+

    E wehe i kahi waihona kikokikona e hiki ai iā ʻoe ke heluhelu a kākau i ka faila. Inā ʻaʻole i loaʻa ka faila, hana ʻia kahi faila hou. Hoʻomaka ka heluhelu ʻana ma ka hoʻomaka ʻana o ka faila, a aia ke kākau ma ke ʻano hoʻohui.

    Inā hoʻoponopono ʻia ka faila binary, e hoʻohana i kēia ʻano komo e hoʻololi i ka mea i luna:

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

    faila pani

    No ka pani ʻana i ka faila, e ʻoluʻolu e hoʻohana i ka hana flose(). ʻO ka prototype o ka hana penei:

    int fclose ( FILE *fp );

    • Inā hoʻopau maikaʻi ka faila, hoʻihoʻi ka hana fclose () i ka zero, a inā hoʻi ka hewa i ka EOF. ʻO kēia hana, ʻoiaʻiʻo, wehe i ka ʻikepili mai ka buffer, pani i ka faila, a hoʻokuʻu i nā hoʻomanaʻo āpau i hoʻohana ʻia no kēlā faila. EOF mea i ho'ākāka 'ia i loko o ka waihona poʻomanaʻo stdio.h

    Hāʻawi ka waihona C maʻamau i nā hana like ʻole e heluhelu a kākau i nā faila ma nā huaʻōlelo a i ʻole he kaula paʻa ka lōʻihi.

    Kākau i ka faila

    Eia nā hana maʻalahi e kākau i nā huaʻōlelo i ke kahawai:

    int fputc ( int c , FILE *fp );

    Na ka hana fputc () e kakau i ka waiwai o ka palena c i loko o ke kahawai puka i kuhikuhi ai ka fp. Inā kūleʻa nā mea kākau, e hoʻihoʻi i ke ʻano kākau a me ka EOF inā loaʻa kahi hewa. Hiki iā ʻoe ke hoʻohana i kēia hana e kākau i kahi kaula e pau ana me ka null i ke kahawai:

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

    Kākau ka hana fputs () i ke kaula s i ke kahawai puka kahi e kuhikuhi ai ka fp. Inā kūleʻa nā mea kākau, e hoʻihoʻi i kahi waiwai ʻole a me ka EOF inā loaʻa kahi hewa. Hiki iā ʻoe ke hoʻohana i ka hana o int fprintf (FILE * fp, const char * format,...) kākau i kahi kaula i ka faila. E ho'āʻo i kēia laʻana:

    'Ōlelo Aʻo: E hōʻoia i loaʻa iā ʻoe kahi papa kuhikuhi tmp, a inā ʻaʻole ia, pono ʻoe e hana mua ia ma kāu kamepiula.

    / tmp he papa kuhikuhi manawa pōkole ma ka ʻōnaehana Linux. Inā holo ʻoe ma ka ʻōnaehana Windows, pono ʻoe e hoʻololi iā ia i kahi papa kuhikuhi i loaʻa i ke kaiapuni kūloko, e like me: C: \ tmp, D: \ tmp, etc.

    laʻana ola

    #hoʻokomo int nui () { FILE *fp = NULL; fp = fopen ("/tmp/test.txt ", "w+"); fprintf (fp , "Ke ho'āʻo nei kēia no fprintf...\n "); fputs ("Ke ho'āʻo nei kēia no fputs...\n ", fp ); flose (fp ); }

    Ke hoʻohui ʻia a hoʻokō ʻia ka code ma luna, hana ia i kahi faila test.txt hou i loko o ka papa kuhikuhi / tmp. A kākau i ʻelua laina me ka hoʻohana ʻana i ʻelua mau hana like ʻole. E heluhelu kākou i kēia faila aʻe.

    Heluhelu i ka faila

    ʻO kēia ka hana maʻalahi loa e heluhelu i kahi huaʻōlelo mai kahi faila:

    int fgetc ( FILE * fp );

    Heluhelu ʻia ka hana fgetc () i kahi ʻano mai ka faila hoʻokomo i nā kiko fp. ʻO ka waiwai hoʻihoʻi ke ʻano heluhelu a me ka EOF inā loaʻa kahi hewa. Hiki iā ʻoe ke heluhelu i kahi string mai kahi kahawai:

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

    Heluhelu ka hana fgets () n-1 mai ke kahawai hookomo i kuhikuhi ia e fp. Hoʻopili ia i ke kaula heluhelu i ka buffer buf a hoʻopili i kahi ʻano null ma ka hope e hoʻopau i ke kaula.

    Inā loaʻa kēia hana i kahi ʻano laina haʻihaʻi '\ n' a i ʻole ka EOF o ka hope o ka faila ma mua o ka heluhelu ʻana i ka huaʻōlelo hope, a laila hoʻi wale i nā huaʻōlelo heluhelu, me nā haki laina. Hiki iā ʻoe ke hoʻohana i ka hana int fscanf (FILE * fp, const char * format,...) e heluhelu i ke kaula mai ka faila, akā pau ka heluhelu ʻana i ka wā e ʻike ai i ka hakahaka mua a me ka haki laina.

    laʻana ola

    #hoʻokomo int nui () { FILE *fp = NULL; char buff [255]; fp = fopen ("/tmp/test.txt "," r "); fscanf (fp , "%s", buff ); printf ("1: %s \n ", hoʻopaʻa ); fgets (buff, 255, (FILE *)fp ); printf ("2: %s \n", buff ); fgets (buff, 255, (FILE *)fp ); printf ("3: %s \n", buff ); flose (fp ); }

    Ke hōʻuluʻulu ʻia a hoʻokō ʻia ka code ma luna, heluhelu ia i nā faila i hana ʻia ma ka ʻāpana mua, e hana ana i kēia mau hopena:

    1: ʻO kēia 2: ke hoʻāʻo nei no ka fprintf...

    3: Ke hoʻāʻo nei kēia no nā fputs...

    ʻO ka mua, heluhelu wale ke ʻano o fscanf() i kēia .no ka mea e hālāwai ana me kahi hakahaka ma hope. ʻO ka lua, e kāhea i ka functon fgets () e heluhelu i ka hapa i koe a hiki i ka hopena o ka laina. ʻO ka hope, e kāhea iā fgets () e heluhelu piha i ka lālani ʻelua.

    Binary I / O hana

    Hoʻohana ʻia nā hana ʻelua no ka hoʻokomo binary a me ka hoʻopuka:

    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 );

    Heluhelu ʻia a kākau ʻia nā hana ʻelua no nā poloka mālama - maʻamau nā arrays a i ʻole nā ​​hale.

    Aia ma luna aʻe e pili ana i ka heluhelu a me ke kākau ʻana i ka faila C no HDV Phoelectron Technology Ltd., kahi hana ʻenehana polokalamu. A ʻo ka hui no nā lako pili pūnaewele (e like me: ACONU/ kamaʻilioONU/ akamaiONU/ puluONU, etc.) ua hui pū i kahi pūʻulu polokalamu ikaika, no kēlā me kēia mea kūʻai aku e hana i nā koi kūʻokoʻa e pono ai, e ʻae i kā mākou huahana i ʻoi aku ka naʻauao a me ka holomua.



    pūnaewele聊天