资源描述:
《c语言文件操作总结》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、C语言文件操作总结FilemanipulationfunctionFileon/offFILE,*infile,=fopen("ride・in〃,〃W〃);Fclose(inf订e);Fileread:Fgetc:Format:intfgetc(FILE^stream);Readsacharacterfromthefilepointingtothefilepointerstream・Thereturnvalueofthisfunctionisabytethatreturnstheread.Retu
2、rnsEOF(-1)ifitisreadattheendofthefileorwhentheerrorisread.Fgets:Theprototypeischar*fgets(char,*s,int,N,FILE,*stream);Parameter:*s:characterpointertothedataaddressthatwillbestored.N:integerdata,whichreadsn-1charactersfromthestream.^stream:pointerdata,t
3、hestreamtoread.Function:ReadnTcharactersfromthefilepointerstream,saveinthespacewheresisthestartingaddress,untilyouhavecompletedarow,andifsuccessful,returnthepointertos,orelsereturnNULL.Fscanf:Functionname:fscanfFunction:performsformattedinputfromastre
4、am,fscanfendswithspacesandlinebreaks,andendswithspaces・Thisisdifferentfromfgets,wherefgetsencountersspacesthatdonotend.Usage:int,fscanf(FILE,^stream,char,^format,[argument,・・・);Intfscanf(filepointer,formatstring,inputlist);Example:fscanf(FP,%s%d%lf,a,
5、&b,&c)Returnvalue:integer,valueequaltothenumberof[argument・・・_Fread:Functionprototypes:size_t,FREAD(void,^buffer,size_t,size,size_t,count,FILE,^stream);Function:readdatafromafilestream,readcountelements,eachelementsizebytes・Ifthecallsucceeds,returncou
6、nt・・Ifthecallissuccessful,theactualreadsize*countbytesarereadParameter:Buffer:isusedtoreceivedatamemoryaddresses,atleastsize*countbytesinsizeSize:thesizeofasingleelement,inbytesThenumberofcount:elements,eachofwhichissizebytes・Stream:inputstreamReturnv
7、alue:thenumberofelementsactuallyread.Ifthereturnvalueisnotthesameascount(notcount*size),theendorerrorofthefilemayoccurGetstheerrorinformationfromferrorandfeof,ordetectsifthefileendsFilewrite:Fputc:Functionname:writethecharacterfilefunctionfputc()Funct
8、ionfunction:writesthecharacterchtothelocationofthecurrentwritingpointerofthefilepointedtobythefilepointerfp.Functionformat:intfputc(int,N,File,*fp)Parameterinterpretation:FPisthefilepointer,whosevalueisobtainedwhenthefileisopenedbyexecutingfop