Inotsanangura kuti C programmer inogadzira, inovhura, uye inovhara sei mameseji faira, kana bhinari faira.
Faera, zvinoreva nhevedzano yemabhaiti, ingave faira remavara kana bhinari faira, C Mutauro, haingopi chete mukana weiyo yepamusoro nhanho mabasa, asiwo inopa iri pasi (OS) kufona kugadzirisa mafaera pane yekuchengetedza mudziyo. . Chitsauko ichi chichatsanangura zvakakosha kufona mukutarisira zvinyorwa.
open-file
Kazhinji kushandisa fopen () basa kugadzira faira idzva kana kuvhura faira iripo, iyi nhare inotanga chinhu chemhando FILE ine ruzivo rwese rwunodiwa kudzora kuyerera. Heino prototype yeiyi basa rekufona:
FILE * fopen ( const char * filename, const char * mode);
Pano filename tambo yekutumidza faira, kukosha kweiyo nzira yekuwana inogona kuve imwe yeanotevera maitiro:
muenzaniso | tsananguro |
r | Vhura faira iripo inobvumira kuti iverengeke. |
w | Vhura text file inobvumira kunyora kufaira. Kana iyo faira isipo, faira nyowani inogadzirwa. Pano, purogiramu yako inonyora zvinyorwa kubva pakutanga kwefaira. Kana iyo faira iripo, ichaderedzwa kusvika zero kureba uye kunyorwa zvakare. |
a | Vhura iyo text file uye nyora kune iyo faira mune append mode. Kana iyo faira isipo, faira nyowani inogadzirwa. Pano, chirongwa chako chinowedzera zvirimo kune mafaera aunotova nawo. |
r+ | Vhura text file inokubvumira kuverenga nekunyora faira racho. |
w+ | Vhura text file inokubvumira kuverenga nekunyora faira racho. Kana iyo faira yatovepo, iyo faira inoderedzwa kusvika zero kureba, uye kana iyo faira isipo, faira nyowani inogadzirwa. |
a+ | Vhura text file inokubvumira kuverenga nekunyora faira racho. Kana iyo faira isipo, faira nyowani inogadzirwa. Iko kuverenga kunotanga pakutanga kwefaira, uye kunyora kunongova mune append mode. |
Kana yakagadziriswa bhinari faira, shandisa inotevera nzira yekuwana kutsiva iri pamusoro:
"rb", "wb", "ab", "rb+", "r+b", "wb+", "w+b", "ab+", "a+b"
yakavharwa faira
Kuvhara iyo faira, ndapota shandisa iyo fclose () basa. Iyo prototype yebasa iri sezvinotevera:
int fclose ( FILE * fp );
- Kana iyo faira yakavharwa zvinobudirira, fclose () basa rinodzorera zero, uye kana kukanganisa kuchidzorera EOF. Iri basa, kutaura zvazviri, rinobvisa iyo data kubva mubhafa, inovhara faira, uye inoburitsa yese ndangariro inoshandiswa kune iyo faira. EOF inogara ichitsanangurwa mumusoro faira stdio.h
Iyo C yakajairwa raibhurari inopa akasiyana mabasa ekuverenga nekunyora mafaera nemabhii kana setambo-yakareba-yakareba.
Nyora kufaira
Heano mabasa akareruka ekunyora mavara kurwizi:
int fputc ( int c, FILE *fp);
Basa fputc () rinonyora kukosha kwehunhu hweparameter c murwizi rwunobuda rwunonongedzwa nefp. Kana iyo inonyora ichibudirira, inodzorera iyo yakanyorwa uye EOF kana kukanganisa kukaitika. Iwe unogona kushandisa rinotevera basa kunyora tambo inopera isina null kurwizi:
int fputs ( const char *s , FILE *fp );
Basa fputs () rinonyora tambo s kune inobuda rwizi uko fp inonongedza. Kana iyo inonyora ikabudirira, inodzosera iyo isiri-negative kukosha uye EOF kana kukanganisa kukaitika. Unogonawo kushandisa basa re int fprintf (FILE * fp, const char * format,...) inonyora tambo kufaira. Edza muenzaniso unotevera:
Cherechedza: Ita shuwa kuti une tmp dhairekitori iripo, uye kana isipo, unofanirwa kuigadzira pakombuta yako kutanga.
/ tmp inowanzo dhairekitori yenguva pfupi paLinux system. Kana iwe uchimhanya paWindows system, unofanirwa kuigadzirisa kune dhairekitori iripo munharaunda yenzvimbo, senge: C: \ tmp, D: \ tmp, nezvimwe.
muenzaniso mupenyu
#include
Kana iyo kodhi iri pamusoro yaunganidzwa uye yaitwa, inogadzira itsva faira test.txt mu/tmp dhairekitori. Uye anonyora kumitsetse miviri achishandisa mabasa maviri akasiyana. Ngativerengei faira iri rinotevera.
Verenga faira
Iri rinotevera ndiro rakareruka basa rekuverenga hunhu mumwechete kubva mufaira:
int fgetc ( FILE * fp );
Iyo fgetc () basa rinoverenga hunhu kubva pafaira rekuisa iro fp inonongedza. Iko kukosha kwekudzoka ndeyekuverenga hunhu uye EOF kana kukanganisa kuchiitika. Basa rinotevera rinokutendera kuti uverenge tambo kubva parukova:
char *fgets ( char *buf , int n , FILE *fp );
Basa racho fgets () rinoverenga n-1 mavara kubva kune inopinza rwizi inotungamirwa nefp. Inokopa tambo yekuverenga kune buffer buf uye inoisa null character kumagumo kumisa tambo.
Kana basa iri richisangana nemutsetse wakaputsika '\ n' kana EOF yekuguma kwefaira usati waverenga chimiro chekupedzisira, zvino unongodzokera kune mavara akaverengwa, kusanganisira kuputsa mutsara. Iwe unogonawo kushandisa int fscanf (FILE * fp, const char * format,...) basa kuverenga tambo kubva mufaira, asi inomira kuverenga paunosangana nekutanga nzvimbo uye mutsara wekuputika.
muenzaniso mupenyu
#include
Kana iyo kodhi iri pamusoro ichiunganidzwa uye ichiitwa, inoverenga mafaera akagadzirwa muchikamu chakapfuura, ichigadzira zvinotevera:
1: Iyi 2: iri kuyedza fprintf...
3: Uku kuyedza fputs ...
Kutanga, iyo nzira ye fscanf() inongoverenga Izvi .nokuti inosangana nenzvimbo kumashure. Chechipiri, danai functon fgets () kuverenga chikamu chasara kusvika pakuguma kwemutsara. Pakupedzisira, dana fgets () kuverenga mutsara wechipiri zvachose.
Binary I / O basa
Aya mabasa maviri anotevera anoshandiswa pakupinza uye kubuda kwebhinari:
size_t fread (isina *ptr, size_t size_of_elements, size_t nhamba_ye_elements, FILE *a_file); size_t fwrite (const void *ptr, size_t size_of_elements, size_t nhamba_ye_elements, FILE *a_file);
Mabasa ese ari maviri anoverengwa uye anonyorerwa mabhuraki ekuchengetedza-kazhinji arrays kana zvimiro.
Pamusoro peC faira kuverenga nekunyora ndeyeHDV Phoelectron Technology Ltd., software yehunyanzvi kushanda. Uye iyo kambani yemidziyo ine hukama netiweki (senge: ACONU/ kutauriranaONU/ akangwaraONU/ fiberONU, etc.) yakaunza pamwe ine simba software timu, kune wese mutengi gadzirisa izvo zvakasarudzika zvinodikanwa vanozvida, zvakare ita kuti zvigadzirwa zvedu zvive nehungwaru uye zvepamberi.