Summary

These notes provide an introduction to file handling in the C programming language. The document discusses different types of data files, file pointers, and their use in file operations.

Full Transcript

Stream. tht with fie lines a...

Stream. tht with fie lines a ukd foRhC) i8 function -The aoren to Used file.) the OfName cified. folloing De the file; the in? oPen (while opened. file perforning ny Befre mutt be file the Slb, File: OPening a *fP FILE declelld ag Canbe pointer File (he stdioh in that FILE tYe vahiable of pointer a Poiter is file -A file. the of Curent Ghd Stotud name, it luding inc fíle, the about things s defined thatinfoymofion Pbinter to is aPointer fiie A pointer: Fle to inforaten of cnttyovd yfe Contain' bloc orande ca filed, data He! foma cotesoy un secomd 1he into data. tws into ivide Subd be CCn file? dota oyiented dtafileg. xieoem en Sy and file ata ted orfen aYe fhere Dhaduction RIAOec9 :sushue. Hondling ile Nodhu? ttemd C-B- Poxt-11 functiong. Plstondard o Usin hed accome lt4 &frle fromYeading oRned, itfile a nce the riting to Fle: oreoatione on otpat Tngt APPend a atb botoriting h reoding g fx filebinây Createa reing for file biny a Pen o both.writing creote APrena oth. wstihg yeodino l fextfile fo a both wYiting andand readhg fx frle text CYeate a both witing and reaing tx file feæt aofen file binay a ab wnting for file binay CYeote a veoding. for fle bina wb Oena file tett APRRnd witin for file text Create a ing. reod for eile teott Open a Meoning Mode opened be wil file filename valid a UP mave how mode the and the determineg file_nome where aracter t string af to aPointer ode char Condt filenarne *-fopen(Const choay Syn: FILE functin foen) {he prototype of The file thal aßociaed PoinieY file the eturng it Ihen Opened Sho he i!scCond the file, t hich inMode tying, tstying, he tuo cePts Tt i.fiyst wit Sho tILES FFle: Fle xecor (on te aLessed o! libray functint through the Rel of Files File Dyes: Peye aetwo tye o fileg. Rey are D Sequentiol File ) Random Accest File Seuential File: Ris type yecords axe exceled seg uentially Ipue cwant to red the lott Grecord af fhe fiie we need to sed all {he YeCor& before thet co 2) Randon Acesg File: This ty Pe chta Can be Sead ar mdife Yordomly TA toke le[s time 4 Comaled to auent ial file. Steps fus File oRyaine: 3 Step,they are ) opening File i) Reodng (o) witing file closing fie. fun File Plo functimk f f r i n Function ofeyalion yead/urite oPevatiom CYeate a file for new fre fopenc) ated with file panter alsoci fclo8e) closeg a fale with fofen) closeall () Closes all opendrles pointer poisition Chah from current ChcDalter. fgete c) Reads the ent n Poibtey to and movey the getc Bame a fgetcl) of data uley to the file fPrintf() wriHes all ty pel Df value f'om a file fscanf) Reads all types ef data writes chaactey to a file fratec Putec) Same a fptec) fPatsc) cnte string to the file Puts) Same al put. Puts) wYiteg an integer to the file getwl) Read! an integey from the file fread c) Reodk structad data written by fenteo. fwrite) Urite bloce af Structuse doto to the file fseelk( ) Set& the Poiotey Po&ition nyele in thefile feofc) Detect the end of file. occured hile read/nte Orertion. ferror) Reforts eor ftellc) Refurng the current Pbin tex pO8ion Yeind) Sets the xecoxd Pointey at the beginin? ofthe file Changei the name of the file function neoning fPrintel) formatte! write to a file f PutcC) cwYite a Characte to o fle f SConf C) Fomate Sead frn a file fgefct) Chraley fyom a file pead a frtsc) wYi e a Sting to a file fyetsc) Read a Stng frm a Ciie fyead l) Yead frongrn afile un.formated Yead foritec) unfosmatted r e {o a file a a ciaratter Rending loritig wsed to Both pute and frutcc) forction ane wrte a chaacter to te fle Tke prototpe of Ptc l) fot pute int ch, FLE *fP)) Poirjes and ch i8 fhe chaac here fp Kthe file oRation is i8 Dcesful, icceKfud, 4 geturnt the to be atput. Le Aute) EOF. Chayalter etuYn, OtesW1Be, eteng a Siniloy, both getc C) &Lfgetcl) functon gete ) i3 charactey fo from file The poDtotype of int gete (FLE *E); U8e of feofc) funetien: end-of-file he feofl) funct{n Can be ued to telt for,Cn Condition- The psctote of the feof() funtiog to seip:) ehaathesy o- ynEnter (n P# Ch); c", PEC 3 tOF) =)!LfP-fgete ) ( Ch whie Jetchcos file:in"); kents cum PCn tre",); fopern("ingo fp L4P); ke felo F) while n; int r; PI*LE iAgtdn ter PA(n Af SET): fseelfe, n, ma) Void file the begining of from y n' saipPing Cfter tent the read and fseek () UBe &ome fle. the _ENDnd Df SEEK thefile ositin of Current CUR Seele erle the Beginning of SeekSet stdioh Jefine macrof, the, of me OYgni8 sitiom, and current new the becomne wilU which OxiSAn and byteg no»of he t'humbytee 8 pojnter, file the +3 fpwhere byf8 un forg *fP, (FLE fseek f int n): int fseeky /8 protoype of The -) f&eek() wth achieved be (on his -file the function eSticuls any actessing andyeding fo Useful are thatfunctioms file The Files toAcce8s Rancom NULL) if(f== l"infotaxt fopen f'= clrscrc); chchar *f FILE ain() nvoid conio:h> #includee Stdioh include< Program w-A:C Pragyuri and file teat OPen JKK to opend: File ese exit(o); "}; exist doeln File printfl NULL) if(f== *);infotxt", fopen (" f= ciYSCYl); *{; FILE maln) Void xi- r"); foren(inso. t*t" fp= Syniax: pointer file compifey (he exist, not doe file the Tf toWOLL for file tirg eit pre- 1h& Read(Y): elxe appendin P I n Filo con be, opend"; o f e n eI d Wbilec) it(c:=gOF) byeal; PEC C, cJ; fclose C¢); etche); wsite HÒ de ( w): Thi8mode oPeng newfile on the disk oveswritten fos writing i4 the ftle alrendy exi? ,it will be with conformaticn ynta: f=fopen( info-txt" "); tertt fiie and Yead it. Prorcn: wnte a Prngram to cwite data to Void mahc) fciose (f); PECn Contenti Yead:"); FILE *f; f-fo Rn (" infotxt char C=; whiiec) clrsreJ; f-fopen("infotnt ; C= gelchect); il e == EOF) break P f n cannot oPen file"); d-c', c); exit(o); PEn wite dta to stop presg': getchl); wh ile (c!=4 cgetcheG); frde(eitp); APPend mode (a): Thi8 mae oPent a pre-existhng file foy nc file s apPending dota LE the fie dont éxi3t then i exikt then the moe of 'a' ofened ie 4 the file docgn't i8 scne c 4) Y4 mode:( rad + ne ) reoding and writing i8 ulcd foY both both WOLL L to the compiler retuns doen' @otist., the IM* TF tBe fe FILE Poin ter infott" Y+"; Syntaa; f= foren(" xead): Contents ae detoy 5) wt (wite+ its if found cyeale file d Return& Tt seacheg fuY file is nt found a hew LFthe file is oPen the file. NUL if fail& to "wt"); txt'", Syotco: f fofen / " info- 6) a+ ( aPPerdt read) Yecord& can be an Rig mode fi'ie Cen be Yead at the end of file. Qdded fopen(" info txt "a+ "); f= Binny MOdes: mode opens a bihcy file in wnte mode. ) wbl write): his in read mode Thi8 mode o Pen &a biny file 2) b (read) y"); Sn: f= fopenl infodat, append node file mde opens a bihay 3) ab(appen): Thi8 dat"a): Sn: f- foren/ "info- tb (rEt:d twte: ini7 mo de in recd and wte mode Ihe f= fopen( infor dat",tb); )otb (veod tike): his made createi C nefile in and wnte mode atb (apend + write): hi8 mode opens e file in apperd mde e dota Can be csiten at the end at file f fiie doeenot eoist new file is created. a The i8 gend from The fofen (). we ned to the opter vadng ond cnting opehtin 1e ove fhe yotas; pelbze (); yot: felosell ); Ris fenction closes the pen fle ond vetang number 314 #define PI conioh)#includeL stdioh) hclude2 civele of area the find to Pog: a withteminated bedetination marto The -> and lon Semi- co Con Identificttor. quick for leHers Capital ithdeclared theje etenplate, macro defineg Stotement Thi% 3-4 #define PI ample: tent) SuLstikute argn) ---fdentifier(aYgi arge #define () identifiex #define text)2sUbstitute Syntax The #define Directive: define Prepyoceskoe Chash) synaol # with beginig the of preproce&a0Y The ’ ands comm ini he the tilised al Pagram SouYe pasked to piler. the he ’ peproLLRS8 & the Poaceske! that DiYectives: (or) sloclej stot fole t #elge stmtzj Stmtl} ier)identif *ifJef2 syntax gerete The condition basedpxogram On the Of the include PYroganmey to the allo directiveg ae (hese inciude #it, areJirectiveg Conpilation Condition ’ and #elfe Compiltion: Gnditin des"stdioh #inclu (oY) cstdioh #include frn genel >Ihe Current inthe file Specified loads directive include The ’ Diecive include # gelec; ae): f", cirle= a ofAxea PfCn SC+4r); ciYclei"; of rodiu Enter \n PFC Clrsre) areaj , flot Cid' dtd' PHC"C= /d ui tcE SdE #elte 3 #ifoef E clYSYC); b,Cid; a,jnt L min) void +define E conio h) tincludec t=inclvdeLstdioh) directive3 meilation ditional uie fo Pog Wnte a Program I: #endif stme); false PE°hisi #el&e stmt"): tyue 8ThiB PeC LINE Fidef clYSCY); mn void LINE I #define onioh ) dee #inclu Stdio tincudee ) efned iey i8 identif whethex the to os not do wwíic charactex control a i8 |iscntyl la) Space bea Otpxintable bt a i3gaph Ld) code AsCil a i d (d) space a i8spcela)isascii digit hexa a is iazdigitla) d decimel diyit Qr)lotter a isalnunld) d digit ibdigitld) letterletIey. capiSmal! kel adias isupper (d) 3 lettey adis isalehacd) HCaning SNO Heoderfilei cctypeh7 Hacros in defined Pre ereial The psogycn the lation af compi during sage ined def user disploy to Dixective #eroY

Use Quizgecko on...
Browser
Browser