资源描述:
《vb读写文件(vb read and write files)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、vb读写文件(VBreadandwritefiles)VBreadandwritefilestousethefollowingstatement:1,Openstatementopensthefile.2,readInput,InputfilesusingLine#,(abovetext)andGet(abovebinarymode).3,writefilesusingPrint#,Write(abovetext)andPut(abovebinarymode).4,Closestatementclosesfile5,binarymodemobilefilelocationusi
2、ngSeekstatement.AllofthesestatementsareexplainedandillustratedindetailwiththehelpofVB.Examplesoftextfiles:Open"TESTFILE"ForOutputAs#1'openstheoutputfile.Print#1,"Thisisatest",writestextdatatoafile.Print#1,'writesblanklinestofiles.Print#1,"Zone1";Tab;"Zone2"dataiswrittentotwozones(printzones)
3、.Print#1,"Hello";"";"World";twostringsseparatedbyspaces.Print#1,Spc(5);"5leadingspaces".Writefivespacesbeforethestring.Print#1,Tab(10);"Hello".Writedataintenthcolumns.'assigningBoolean,Date,NullandErroretc..DimMyBool,MyDate,MyNull,MyErrorMyBool=False:MyDate=#February12,1969#:MyNull=NullMyErr
4、or=CVErr(32767)'True,False,NullandErrorautomaticallyconvertformatsaccordingtothelocalesettingsofthesystem.'datewillbeshowninthestandardshortdateformat.Print#1,MyBool;"isaBooleanvalue""Print#1,MyDate;"isadate""Print#1,MyNull;"isanullvalue""Print#1,MyError;"isanerrorvalue""Close#1'closefile.Ex
5、ampleofreadingfileUsetheLineInput#statementreadsalinefromasequentialfile,andthedatatoavariable.ThisexampleassumesthattheTESTFILEfilecontainsthenumberoflinesoftextdata.DimTextLineOpen"TESTFILE"ForInputAs#1'opensthefile.DoWhileNotEOF(1)'cycletofiletail.LineInput#1,TextLine'readsarowofdataandgi
6、vesittoavariable.Debug.PrintTextLine'displaysdatainthedebugwindow.LoopClose#1'closefile.Youcanalsofindmanyoftheseproblemsandexamplesinour"disk,fileanddirectory"column.GetandPutstatementscanalsoreadandwritemultiplebytes,andeachreadandwritecontentcanbeplacedinabytevariablearraytoimprovetheprog
7、ramspeed.Forexample:DimDSX()AsByte'isanarrayofbytesusedtostorereadandwritecontentDimReadFileNo,WriteFileNoAsInteger'readwritefilenumber'ConstUnit=100000'readandwriteblocksizeOpenSourceFileNameForBinaryAccessReadAs1WriteFileNo=FreeFileOpenTargetFile