Dondora uburyo C programmer irema, ifungura, ikanafunga inyandiko, cyangwa dosiye ebyiri.
Idosiye, isobanura urukurikirane rwa bytes, yaba dosiye yinyandiko cyangwa dosiye ebyiri, C Ururimi, ntabwo itanga gusa uburyo bwo kugera kumikorere yo murwego rwo hejuru, ahubwo inatanga guhamagarwa (OS) guhamagarira gutunganya dosiye kububiko. . Iki gice kizasobanura guhamagarwa kwingenzi mugucunga inyandiko.
fungura dosiye
Mubisanzwe ukoresheje imikorere ya fopen () kugirango ukore dosiye nshya cyangwa ufungure dosiye ihari, ihamagarwa ritangiza ikintu cyubwoko bwa FILE gikubiyemo amakuru yose akenewe kugirango ugenzure imigendekere. Dore prototype yiyi mikorere hamagara:
FILE * fopen (const char * filename, const char * uburyo);
Hano filename ni umugozi wo kwita izina dosiye, agaciro k'uburyo bwo kwinjira karashobora kuba imwe mumico ikurikira:
icyitegererezo | ibisobanuro |
r | Fungura dosiye iriho inyandiko yemerera gusomwa. |
w | Fungura inyandiko yinyandiko yemerera kwandika kuri dosiye. Niba dosiye idahari, hashyizweho dosiye nshya. Hano, gahunda yawe yandika ibirimo kuva intangiriro ya dosiye. Niba dosiye ibaho, izagabanywa kugeza kuri zeru hanyuma yongere yandike. |
a | Fungura inyandiko hanyuma wandike dosiye muburyo bwumugereka. Niba dosiye idahari, hashyizweho dosiye nshya. Hano, porogaramu yawe yongeramo ibiri muri dosiye usanzwe ufite. |
r+ | Fungura inyandiko yinyandiko igufasha gusoma no kwandika dosiye. |
w+ | Fungura inyandiko yinyandiko igufasha gusoma no kwandika dosiye. Niba dosiye isanzweho, dosiye yagabanijwe kugeza kuri zeru, kandi niba dosiye idahari, hashyizweho dosiye nshya. |
a+ | Fungura inyandiko yinyandiko igufasha gusoma no kwandika dosiye. Niba dosiye idahari, hashyizweho dosiye nshya. Gusoma bitangira intangiriro ya dosiye, kandi kwandika biri muburyo bwumugereka. |
Niba itunganijwe rya binary dosiye, koresha uburyo bukurikira bwo gusimbuza ibyavuzwe haruguru:
"rb", "wb", "ab", "rb +", "r + b", "wb +", "w + b", "ab +", "a + b"
dosiye ifunze
Gufunga dosiye, nyamuneka koresha imikorere ya fclose (). Porotype yimikorere niyi ikurikira:
int fclose (FILE * fp);
- Niba dosiye ifunze neza, imikorere ya fclose () isubiza zeru, kandi niba ikosa risubiza EOF. Iyi mikorere, mubyukuri, ikuraho amakuru muri buffer, ifunga dosiye, kandi irekura ububiko bwose bwakoreshejwe kuri iyo dosiye. EOF nibisanzwe bisobanurwa mumutwe wa dosiye stdio.h
Isomero C risanzwe ritanga imirimo itandukanye yo gusoma no kwandika dosiye ukoresheje inyuguti cyangwa nkumugozi muremure.
Andika kuri dosiye
Dore imikorere yoroshye yo kwandika inyuguti kumurongo:
int fputc (int c, FILE * fp);
Imikorere fputc () yandika inyuguti yagaciro ya parameter c mumasoko asohoka fp yerekeza. Niba ibyanditswe byatsinze, isubiza inyuguti yanditse na EOF niba habaye ikosa. Urashobora gukoresha imikorere ikurikira kugirango wandike umurongo urangirana nubusa kumugezi:
int fputs (const char * s, FILE * fp);
Imikorere fputs () yandika umugozi s kumasoko asohoka aho fp yerekeza. Niba ibyanditse bigenda neza, bisubiza agaciro katari keza na EOF niba habaye ikosa. Urashobora kandi gukoresha imikorere ya int fprintf (FILE * fp, form char * format, ...) yandika umurongo kuri dosiye. Gerageza urugero rukurikira:
Icyitonderwa: Menya neza ko ufite ububiko bwa tmp buboneka, kandi niba butabaho, ugomba kubanza kubikora kuri mudasobwa yawe.
/ tmp mubusanzwe ububiko bwigihe gito kuri sisitemu ya Linux. Niba ukoresha kuri sisitemu ya Windows, ugomba kuyihindura mububiko buriho mubidukikije, nka: C: \ tmp, D: \ tmp, nibindi.
urugero ruzima
#shyiramo
Iyo code yavuzwe haruguru ikusanyirijwe hamwe ikanashyirwa mubikorwa, ikora dosiye nshya ya test.txt inthe / tmp. Kandi yandika kumirongo ibiri ukoresheje imirimo ibiri itandukanye. Reka dusome iyi dosiye ubutaha.
Soma dosiye
Ibikurikira nigikorwa cyoroshye cyo gusoma inyuguti imwe kuva muri dosiye:
int fgetc (FILE * fp);
Imikorere ya fgetc () isoma inyuguti kuva muri dosiye yinjiza aho fp yerekeza. Garuka agaciro nugusoma inyuguti na EOF niba habaye ikosa. Imikorere ikurikira iragufasha gusoma umurongo uva kumugezi:
char * fgets (char * buf, int n, FILE * fp);
Imikorere fgets () isoma n-1 inyuguti ziva kumurongo winjizwa na fp. Yandukuye umugozi wasomwe kuri buffer buf kandi yongeramo inyuguti zidafite ishingiro kurangiza umurongo.
Niba iyi mikorere ihuye numurongo wacitse '\ n' cyangwa EOF yanyuma ya dosiye mbere yo gusoma inyuguti yanyuma, hanyuma ugasubira gusa mubisomwa byasomwe, harimo gucamo umurongo. Urashobora kandi gukoresha int fscanf (FILE * fp, const char * format, ...) imikorere kugirango usome umurongo uva muri dosiye, ariko ihagarika gusoma mugihe uhuye numwanya wambere no gutandukana kumurongo.
urugero ruzima
#shyiramo
Iyo code yavuzwe haruguru ikusanyirijwe hamwe ikanakorwa, isoma dosiye zakozwe mugice kibanziriza iki, zitanga ibisubizo bikurikira:
1: Iyi 2: ni kugerageza kuri fprintf ...
3: Ibi ni kugerageza fputs ...
Ubwa mbere, uburyo bwa fscanf () busoma gusa Ibi .kuko bihura n'umwanya inyuma. Icyakabiri, hamagara imikorere ya fgets () kugirango usome igice gisigaye kugeza umurongo urangiye. Hanyuma, hamagara fgets () kugirango usome umurongo wa kabiri rwose.
Imikorere ya Binary I / O.
Ibikorwa bibiri bikurikira bikoreshwa muburyo bubiri bwinjiza nibisohoka:
ubunini_tubusa (ubusa * ptr, ubunini_tubunini_ibintu, ubunini_tumubare_ibikoresho, FILE * a_file); ubunini_twandika (const void * ptr, size_t size_of_elements, size_t numero_ibikoresho, FILE * a_file);
Imikorere yombi irasomwa kandi yandike kubikwa-mubisanzwe imirongo cyangwa imiterere.
Hejuru kubyerekeye C dosiye yo gusoma no kwandika ni iya HDV Phoelectron Technology Ltd, imikorere ya software. Nisosiyete kubikoresho bijyanye nurusobe (nka: ACONU/ itumanahoONU/ umunyabwengeONU/ fibreONU, nibindi) yahurije hamwe itsinda rikomeye rya software, kuri buri mukiriya yihitiramo ibyifuzo byihariye abikeneye, kandi reka ibicuruzwa byacu birusheho kugira ubwenge kandi byateye imbere.