[ Pobierz całość w formacie PDF ] .The following command opens the file c:\samples\vb\cust.dat as a sequential file with a num-ber obtained through the FreeFile() function:Dim Fnum As Integer = FreeFile()FileOpen(Fnum, c:\samples\vb\cust.dat , OpenMode.Output, OpenAccess.ReadWrite )FILE I/O chF43FileClose(file_number)The FileClose() function closes an open file, whose number is passed as argument.The statementFileClose(fNum1)closes the file opened as fNum1.Reset()The Reset() function closes all files opened with FileOpen().Use this statement to close all the filesopened by your application.EOF(file_number), LOF(file_number)These are two more frequently used functions in file manipulation.The EOF() function accepts asan argument the number of an open file and returns True if the end of the file (EOF) has beenreached.The LOF() function returns the length of the file, whose number is passed as argument.You use the EOF() function to determine whether the end of the file has been reached, with aloop such as the following:{get first record}While Not EOF(fNum){ process current record }{ get next record }End WhileWith the help of the LOF() function, you can also calculate the number of records in a random-access file:Rec_Length = LOF(file_number) / Len(record)Print (file_number, output_list), PrintLine(file_number, output_list)The Print() function writes data to a sequential file.The first argument is the number of the file tobe written, and the following arguments are the values, or variables, to be written to the file.The sec-ond argument is a parameter array, which means you can pass any number of values to the function:Print(fNum, var1, var2, some literal , 333.333)The Print() function doesn t insert line breaks between successive calls.The following statementswrite a single line of text to the file opened as fNum:Print(fNum, "This is the first half of the line ")Print(fNum, "and this is the second half of the same line.")The PrintLine() function does the same thing as the Print() function, but it also adds a newline character at the end of the values it writes to the file.Its syntax is identical to the syntax of thePrint() function.Multiple values are separated by commas, and each comma specifies that the nextcharacter will be printed in the next print zone.Each print zone corresponds to 14 columns.In otherwords, the Print() function writes data to the file exactly as the TYPE command (of DOS) displayschF44 BONUS REFERENCE VB.NET FUNCTIONS AND STATEMENTSthem on the screen.(That s why the data saved by the Print() function are called display-formatteddata.) You must keep in mind that the text will be displayed correctly only when printed with amonospaced typeface, such as Courier.If you place the text on a TextBox with a proportional type-face, the columns will not align.Data saved with the Print() function can be read with the LineInput() and Input() functions.However, isolated fields are not delimited in any way, and you must extract the fields from the lineread.The Print() function is used to create text files that can be viewed on a DOS window.To for-mat the fields on each line, you can use the Tab to position the pointer at the next print zone, orTab(n) to position the pointer at an absolute column number.The following statements create atext file:On Error Resume NextKill( c:\test.txt )Dim fNum As Integer = FreeFile()FileOpen(fNum, c:\test.txt , OpenMode.Output)PrintLine(fNum, John , TAB(12), Ashley , TAB(25), Manager , TAB(45), 33)PrintLine(fNum, Michael , TAB(12), Staknovitch , TAB(25), Programmer , _TAB(45), 28)PrintLine(fNum, Tess , TAB(12), Owen , TAB(25), Engineer , TAB(45), 41)PrintLine(fNum, Joe , TAB(12), Dow , TAB(25), Administrator , TAB(45), 25)PrintLine(fNum, ************************* )PrintLine(fNum, John , TAB, Ashley , TAB, Manager , TAB, 3)PrintLine(fNum, Michael , TAB, Staknovitch , TAB, Programmer , TAB, 28)PrintLine(fNum, Tess , TAB, Owen , TAB, Engineer , TAB, 41)PrintLine(fNum, Joe , TAB, Dow , TAB, Administrator , TAB, 25)FileClose(fNum)This is the output produced by this example:John Ashley Manager 33Michael Staknovitch Programmer 28Tess Owen Engineer 41Joe Dow Administrator 25*************************John Ashley Manager 33Michael Staknovitch Programmer 28Tess Owen Engineer 41Joe Dow Administrator 25Input(file_number, var)The Input() function reads data from a sequential file and assigns them to the variable passed withthe second argument The following lines read two values from the open file, a numeric value anda date:Dim numVal As Long, DateVal As DateInput(1, numVal)Input(1, DateVal)FILE I/O chF45LineInput(file_number)To read from sequential files, use the LineInput() function.The file_number argument is the file snumber, and the function returns the next text line in the file.This statement reads all the charactersfrom the beginning of the file to the first newline character.When you call it again, it returns thefollowing characters, up to the next newline character.The newline characters are not part of theinformation stored to or read from the file, and they are used only as delimiters.If we close the fileof the last example and open it again, the following lines will read the first two text lines and assignthem to the string variables Line1 and Line2:Line1 = LineInput(fNum)Line2 = LineInput(fNum)If you want to store plain text to a disk file, create a sequential file and store the text there, oneline at a time.To read it back, open the file and read one line at a time with the LineInput() func-tion, or use the FileGet() function to read the entire text
[ Pobierz całość w formacie PDF ] zanotowane.pldoc.pisz.plpdf.pisz.plmikr.xlx.pl
|