Yana bayyana yadda mai shirye-shiryen C ke ƙirƙira, buɗewa, da rufe fayil ɗin rubutu, ko fayil ɗin binary.
Fayil, yana nufin jerin bytes, ko fayil ɗin rubutu ne ko fayil ɗin binary, C Harshe, ba wai kawai yana ba da damar yin amfani da ayyuka na matakin sama ba, har ma yana ba da kira na asali (OS) don aiwatar da fayiloli akan na'urar ajiya. . Wannan babin zai bayyana mahimman kira a cikin sarrafa takardu.
bude fayil
Yawancin lokaci ta yin amfani da aikin fopen () don ƙirƙirar sabon fayil ko buɗe fayil ɗin da ke akwai, wannan kiran yana fara wani abu na nau'in FILE wanda ya ƙunshi duk mahimman bayanai don sarrafa kwararar. Ga samfurin wannan kiran aikin:
FILE * fopen (const char * sunan fayil, yanayin char * yanayin);
Anan filename shine kirtani don suna fayil, ƙimar yanayin shiga na iya zama ɗaya daga cikin dabi'u masu zuwa:
tsari | bayanin |
r | Bude fayil ɗin rubutu da ke akwai wanda ke ba da damar karanta shi. |
w | Bude fayil ɗin rubutu wanda ke ba da damar rubutu zuwa fayil ɗin. Idan babu fayil ɗin, ana ƙirƙiri sabon fayil. Anan, shirin ku yana rubuta abun ciki daga farkon fayil ɗin. Idan fayil ɗin ya wanzu, za a yanke shi zuwa tsayin sifili kuma a sake rubuta shi. |
a | Bude fayil ɗin rubutu kuma rubuta zuwa fayil ɗin a cikin yanayin kari. Idan babu fayil ɗin, ana ƙirƙiri sabon fayil. Anan, shirin ku yana haɗa abun ciki zuwa fayilolin da kuke da su. |
r+ | Bude fayil ɗin rubutu wanda zai baka damar karantawa da rubuta fayil ɗin. |
w+ | Bude fayil ɗin rubutu wanda zai baka damar karantawa da rubuta fayil ɗin. Idan fayil ɗin ya riga ya wanzu, an yanke fayil ɗin zuwa tsayin sifili, kuma idan fayil ɗin ba ya wanzu, an ƙirƙiri sabon fayil. |
a+ | Bude fayil ɗin rubutu wanda zai baka damar karantawa da rubuta fayil ɗin. Idan babu fayil ɗin, ana ƙirƙiri sabon fayil. Karatun yana farawa a farkon fayil ɗin, kuma rubutun yana cikin yanayin kari ne kawai. |
Idan fayil ɗin binary ɗin da aka sarrafa, yi amfani da yanayin samun dama don maye gurbin na sama:
"rb", "b", "ab", "rb+", "r+b", "wb+", "w+b", "ab+", "a+b"
rufaffiyar fayil
Don rufe fayil ɗin, da fatan za a yi amfani da aikin fclose(). Samfurin aikin shine kamar haka:
int fclose ( FILE * fp );
- Idan an rufe fayil ɗin cikin nasara, aikin fclose() zai dawo da sifili, kuma idan kuskuren ya dawo EOF. Wannan aikin, a haƙiƙa, yana cire bayanai daga ma'ajin, rufe fayil ɗin, kuma yana fitar da duk ƙwaƙwalwar ajiyar da aka yi amfani da ita don wannan fayil ɗin. EOF ne akai-akai a cikin fayil na kai stdio.h
Madaidaicin ɗakin karatu na C yana ba da ayyuka daban-daban don karantawa da rubuta fayiloli ta haruffa ko azaman tsayayyen kirtani.
Rubuta zuwa fayil
Anan akwai ayyuka mafi sauƙi don rubuta haruffa zuwa rafi:
int fputc ( int c , FILE * fp );
Aikin fputc () yana rubuta ƙimar siga c cikin rafin fitarwa wanda fp ke nunawa. Idan rubuce-rubucen sun yi nasara, zai dawo da rubutun da aka rubuta da EOF idan kuskure ya faru. Kuna iya amfani da aikin mai zuwa don rubuta kirtani mai ƙarewa tare da ɓarna zuwa rafi:
int fputs (const char *s, FILE *fp);
Ayyukan fputs () yana rubuta kirtani s zuwa rafin fitarwa inda fp ke nunawa. Idan rubutun ya yi nasara, zai dawo da ƙimar mara kyau da EOF idan kuskure ya faru. Hakanan zaka iya amfani da aikin int fprintf (FILE * fp, const char * tsarin,...) yana rubuta kirtani zuwa fayil ɗin. Gwada misali mai zuwa:
Lura: Tabbatar cewa kana da babban directory tmp, kuma idan babu shi, kana buƙatar ƙirƙirar shi a kan kwamfutarka da farko.
/ tmp yawanci adireshi ne na wucin gadi akan tsarin Linux. Idan kuna aiki akan tsarin Windows, kuna buƙatar canza shi zuwa wurin da ya wanzu a cikin mahallin gida, kamar: C: \ tmp, D: \ tmp, da sauransu.
misali mai rai
#hada da
Lokacin da aka haɗa lambar da ke sama kuma aka aiwatar da ita, tana ƙirƙirar sabon gwajin fayil.txt inthe/tmp directory. Kuma ya rubuta zuwa layi biyu ta amfani da ayyuka daban-daban guda biyu. Bari mu karanta wannan fayil na gaba.
Karanta fayil ɗin
Mai zuwa shine mafi sauƙin aiki don karanta harafi ɗaya daga fayil:
int fgetc (FILE * fp);
Aikin fgetc () yana karanta haruffa daga fayil ɗin shigarwa wanda fp ke nunawa. Ƙimar dawowa ita ce halin karantawa da EOF idan kuskure ya faru. Ayyukan da ke biyowa suna ba ku damar karanta kirtani daga rafi:
char * fgets ( char * buf , int n , FILE * fp );
Aikin fgets () yana karanta haruffa n-1 daga rafin shigar da fp ke jagoranta. Yana kwafin kirtani da aka karanta zuwa ga buffer buffer kuma yana ƙara haruffa mara kyau a ƙarshen don ƙare kirtan.
Idan wannan aikin ya ci karo da harafin layin da aka karye '\ n' ko EOF na ƙarshen fayil ɗin kafin karanta harafin ƙarshe, sannan kawai ya koma cikin haruffan da aka karanta, gami da hutun layi. Hakanan zaka iya amfani da aikin int fscanf (FILE * fp, const char * tsarin,...) don karanta kirtani daga fayil ɗin, amma yana daina karantawa lokacin da aka ci karo da sarari na farko da hutun layi.
misali mai rai
#hada da
Lokacin da aka haɗa lambar da ke sama kuma aka aiwatar da ita, tana karanta fayilolin da aka ƙirƙira a sashin da ya gabata, yana samar da sakamako masu zuwa:
1: Wannan 2: gwaji ne don fprintf...
3: Wannan gwaji ne don fputs ...
Na farko, hanyar fscanf () tana karanta Wannan .saboda ya ci karo da sarari a baya. Na biyu, kira functon fgets () don karanta ragowar sashin har zuwa ƙarshen layin. A ƙarshe, kira fgets () don karanta jere na biyu gaba ɗaya.
Binary I / O aiki
Ana amfani da ayyuka guda biyu masu zuwa don shigarwa da fitarwa na binary:
size_t fread (void *ptr, size_t size_of_elements, size_t yawan_elements, FILE *a_file); size_t fwrite (const void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file);
Dukansu ayyuka ana karantawa kuma ana rubuta su don tubalan ajiya - yawanci tsararraki ko tsari.
Sama game da karatun fayil da rubutu na HDV Phoelectron Technology Ltd., aikin fasaha na software. Kuma kamfani don kayan aiki masu alaƙa da hanyar sadarwa (kamar: ACONU/ sadarwaONU/ mai hankaliONU/ fiberONU, da dai sauransu) ya haɗu da ƙungiyar software mai ƙarfi, ga kowane abokin ciniki ya keɓanta keɓaɓɓen buƙatun waɗanda suke buƙata, kuma bari samfuranmu su zama masu hankali da haɓaka.