文件操作主要函数Word文档下载推荐.docx
《文件操作主要函数Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《文件操作主要函数Word文档下载推荐.docx(27页珍藏版)》请在冰豆网上搜索。
Libraries
LIBC.LIB
Singlethreadstaticlibrary,retailversion
LIBCMT.LIB
Multithreadstaticlibrary,retailversion
MSVCRT.LIB
ImportlibraryforMSVCRT.DLL,retailversion
Thec,n,andtmodeoptionsareMicrosoftextensionsforfopenand_fdopenandshouldnotbeusedwhereANSIportabilityisdesired.
ReturnValue
Eachofthesefunctionsreturnsapointertotheopenfile.Anullpointervalueindicatesanerror.
Parameters
filename
Filename
mode
Typeofaccesspermitted
Remarks
Thefopenfunctionopensthefilespecifiedbyfilename._wfopenisawide-characterversionoffopen;
theargumentsto_wfopenarewide-characterstrings._wfopenandfopenbehaveidenticallyotherwise.
Generic-TextRoutineMappings
TCHAR.HRoutine
_UNICODE&
_MBCSNotDefined
_MBCSDefined
_UNICODEDefined
_tfopen
Thecharacterstringmodespecifiesthetypeofaccessrequestedforthefile,asfollows:
"
r"
Opensforreading.Ifthefiledoesnotexistorcannotbefound,thefopencallfails.
w"
Opensanemptyfileforwriting.Ifthegivenfileexists,itscontentsaredestroyed.
a"
Opensforwritingattheendofthefile(appending)withoutremovingtheEOFmarkerbeforewritingnewdatatothefile;
createsthefilefirstifitdoesn’texist.
r+"
Opensforbothreadingandwriting.(Thefilemustexist.)
w+"
Opensanemptyfileforbothreadingandwriting.Ifthegivenfileexists,itscontentsaredestroyed.
a+"
Opensforreadingandappending;
theappendingoperationincludestheremovaloftheEOFmarkerbeforenewdataiswrittentothefileandtheEOFmarkerisrestoredafterwritingiscomplete;
Whenafileisopenedwiththe"
or"
accesstype,allwriteoperationsoccurattheendofthefile.Thefilepointercanberepositionedusingfseekorrewind,butisalwaysmovedbacktotheendofthefilebeforeanywriteoperationiscarriedout.Thus,existingdatacannotbeoverwritten.
The"
modedoesnotremovetheEOFmarkerbeforeappendingtothefile.Afterappendinghasoccurred,theMS-DOSTYPEcommandonlyshowsdatauptotheoriginalEOFmarkerandnotanydataappendedtothefile.The"
modedoesremovetheEOFmarkerbeforeappendingtothefile.Afterappending,theMS-DOSTYPEcommandshowsalldatainthefile.The"
modeisrequiredforappendingtoastreamfilethatisterminatedwiththeCTRL+ZEOFmarker.
Whenthe"
"
or"
accesstypeisspecified,bothreadingandwritingareallowed(thefileissaidtobeopenfor“update”).However,whenyouswitchbetweenreadingandwriting,theremustbeaninterveningfflush,fsetpos,fseek,orrewindoperation.Thecurrentpositioncanbespecifiedforthefsetposorfseekoperation,ifdesired.
Inadditiontotheabovevalues,thefollowingcharacterscanbeincludedinmodetospecifythetranslationmodefornewlinecharacters:
t
Openintext(translated)mode.Inthismode,CTRL+Zisinterpretedasanend-of-filecharacteroninput.Infilesopenedforreading/writingwith"
fopenchecksforaCTRL+Zattheendofthefileandremovesit,ifpossible.ThisisdonebecauseusingfseekandftelltomovewithinafilethatendswithaCTRL+Z,maycausefseektobehaveimproperlyneartheendofthefile.
Also,intextmode,carriagereturn–linefeedcombinationsaretranslatedintosinglelinefeedsoninput,andlinefeedcharactersaretranslatedtocarriagereturn–linefeedcombinationsonoutput.WhenaUnicodestream-I/Ofunctionoperatesintextmode(thedefault),thesourceordestinationstreamisassumedtobeasequenceofmultibytecharacters.Therefore,theUnicodestream-inputfunctionsconvertmultibytecharacterstowidecharacters(asifbyacalltothembtowcfunction).Forthesamereason,theUnicodestream-outputfunctionsconvertwidecharacterstomultibytecharacters(asifbyacalltothewctombfunction).
b
Openinbinary(untranslated)mode;
translationsinvolvingcarriage-returnandlinefeedcharactersaresuppressed.
Iftorbisnotgiveninmode,thedefaulttranslationmodeisdefinedbytheglobalvariable_fmode.Iftorbisprefixedtotheargument,thefunctionfailsandreturnsNULL.
FormoreinformationaboutusingtextandbinarymodesinUnicodeandmultibytestream-I/O,seeTextandBinaryModeFileI/OandUnicodeStreamI/OinTextandBinaryModes.
c
Enablethecommitflagfortheassociatedfilenamesothatthecontentsofthefilebufferarewrittendirectlytodiskifeitherfflushor_flushalliscalled.
n
Resetthecommitflagfortheassociatedfilenameto“no-commit.”Thisisthedefault.ItalsooverridestheglobalcommitflagifyoulinkyourprogramwithCOMMODE.OBJ.Theglobalcommitflagdefaultis“no-commit”unlessyouexplicitlylinkyourprogramwithCOMMODE.OBJ.
Validcharactersforthemodestringusedinfopenand_fdopencorrespondtooflagargumentsusedin_openand_sopen,asfollows.
CharactersinmodeString
EquivalentoflagValuefor_open/_sopen
a
_O_WRONLY|_O_APPEND(usually_O_WRONLY|_O_CREAT|_O_APPEND)
a+
_O_RDWR|_O_APPEND(usually_O_RDWR|_O_APPEND|_O_CREAT)
r
_O_RDONLY
r+
_O_RDWR
w
_O_WRONLY(usually_O_WRONLY|_O_CREAT|_O_TRUNC)
w+
_O_RDWR(usually_O_RDWR|_O_CREAT|_O_TRUNC)
_O_BINARY
_O_TEXT
None
Example
/*FOPEN.C:
Thisprogramopensfilesnamed"
data"
*and"
data2"
.Itusesfclosetoclose"
and
*_fclosealltocloseallremainingfiles.
*/
#include<
FILE*stream,*stream2;
voidmain(void)
{
intnumclosed;
/*Openforread(willfailiffile"
doesnotexist)*/
if((stream=fopen("
))==NULL)
printf("
Thefile'
data'
wasnotopened\n"
);
else
wasopened\n"
/*Openforwrite*/
if((stream2=fopen("
data2'
/*Closestream*/
if(fclose(stream))
wasnotclosed\n"
/*Allotherfilesareclosed:
numclosed=_fcloseall();
Numberoffilesclosedby_fcloseall:
%u\n"
numclosed);
}
Output
wasopened
1
StreamI/ORoutines
SeeAlso
fclose,_fdopen,ferror,_fileno,freopen,_open,_setmode
fclose,_fcloseall
Closesastream(fclose)orclosesallopenstreams(_fcloseall).
intfclose(FILE*stream);
int_fcloseall(void);
fclose
_fcloseall
fclosereturns0ifthestreamissuccessfullyclosed._fcloseallreturnsthetotalnumberofstreamsclosed.BothfunctionsreturnEOFtoindicateanerror.
Parameter
stream
PointertoFILEstructure
Thefclosefunctionclosesstream._fcloseallclosesallopenstreamsexceptstdin,stdout,stderr(and,inMS-DOS®
_stdauxand_stdprn).Italsoclosesanddeletesanytemporaryfilescreatedbytmpfile.Inbothfunctions,allbuffersassociatedwiththestreamareflushedpriortoclosing.System-allocatedbuffersarereleasedwhenthestreamisclosed.Buffersassignedbytheuserwithsetbufandsetvbufarenotautomaticallyreleased.
_close,_fdopen,fflush,fopen,freopen
fwrite
Writesdatatoastream.
size_tfwrite(constvoid*buffer,size_tsize,size_tcount,FILE*stream);
fwritereturnsthenumberoffullitemsactuallywritten,whichmaybelessthancountifanerroroccurs.Also,ifanerroroccurs,thefile-positionindicatorcannotbedetermined.
buffer
Pointertodatatobewritten
size
Itemsizeinbytes
count
Maximumnumberofitemstobewritten
Thefwritefunct