Information about Open Files


Information about Open Files

OOo Basic has functions that return file information by using the file name (see Listing 6). It is also possible to obtain information about open files from the file number. The FileAttr function returns how the file associated with the given file number was opened. Table 10 lists the return values and their meanings.

Table 10: Valid protected keywords.

Return Value

Description

1

Open For Input

2

Open For Output

4

Open For Random

8

Open for Append

16

Open For Binary

 FileAttr(n, 1)  'How the file was opened in BASIC using Open For ... 
Bug  

According to the OOo help, FileAttr(n, 2) returns an operating-system-specific number that is not available on all operating systems. In reality, however, if the second argument is not 1, the return value is always 0. Another problem is that the included help incorrectly states that Binary has a return value of 32.

Use the EOF function to determine if the end of the file has been reached. A typical use is to read all of the data until "End Of File."

 n = FreeFile                    'Always find the next free file number Open FileName For Input As #n   'Open the file for input Do While NOT EOF(n)             'While NOT End Of File   Input #n, s                   'Read some data!   REM Process the input here! Loop 

Use the LOF function to determine the length of an open file. This number is always in bytes.

 LOF(n) 

Use the Loc function to obtain the current location of the file pointer. This number is not always accurate and the return value has a different meaning depending on how the file was opened. Loc returns the actual byte position for files opened in Binary mode. For Random files, Loc returns the record number of the last record read or written. For sequential files opened with Input, Output, or Append, however, the number returned by Loc is the current byte position divided by 128. This is done to maintain compatibility with other versions of BASIC.

 Loc (n) 
Bug  

If a file is opened in a mode other than Random, and OOo Basic considers the file a text file, Loc returns the line number that will be read next. I cannot decide if this is a bug or just incomplete documentation. Sometimes the return values from Loc are just wrong. For example, if you open a file for output and then write some text, Loc returns 0.

The Seek function, when used with only one argument, returns the next position that will be read or written. This is similar to the Loc function except that for sequential files, the absolute byte position is always returned. If you want to save the position in a file and return to it later, use the Seek function to obtain the current file pointer; then you can use the Seek function to return the file pointer to the original location.

 position = Seek(n)     'Obtain and save the current position. statements             'Do arbitrary stuff. Seek(n, position)      'Move the file pointer back to the original position. 

The argument for setting the file pointer using the Seek function is the same as the value returned by the Seek function. For Random files, the position is the number of the object to read, not the byte position. For Sequential files, the position is the byte position in the file. The macro in Listing 9 returns information about an open file from the file number, including the open mode, file length, and file pointer location.

Listing 9: GetOpenFilelnfo is found in the File module in this chapter's source code files as SC07.sxw.
start example
 Function GetOpenFileInfo(n As Integer) As String   Dim s As String   Dim iAttr As Integer   On Error GoTo BadFileNumber iAttr = FileAttr(n, 1)   If iAttr = 0 Then     s = "File handle " & CStr(n) & " is not currently open" & CHR$(10)   Else     s = "File handle " & CStr(n) & " was opened in mode:"     If (iAttr AND 1)  =  1 Then s = s & " Input"     If (iAttr AND 2)  =  2 Then s = s & " Output"     If (iAttr AND 4)  =  4 Then s = s & " Random"     If (iAttr AND 8)  =  8 Then s = s & " Append"     If (iAttr AND 16) = 16 Then s = s & " Binary"     iAttr = iAttr AND NOT (1 OR 2 OR 4 OR 8 OR 16)     If iAttr AND NOT (1 OR 2 OR 4 OR 8 OR 16) <> 0 Then       s = s & " unsupported attribute " & CStr(iAttr)     End If     s = s & CHR$(10)     s = s & "File length = " & nPrettyFileLen(LOF(n)) & CHR$(10)     s = s & "File location = " & CStr(LOC(n)) & CHR$(10)     s = s & "Seek = " & CStr(Seek(n)) & CHR$(10)     s = s & "End Of File = " & CStr(EOF(n)) & CHR$(10)   End If AllDone:   On Error GoTo 0   GetOpenFileInfo = s   Exit Function BadFileNumber:   s = s & "Error with file handle " & CStr(n) & CHR$(10) &_           "The file is probably not open" & CHR$(10) & Error()   Resume AllDone End Function 
end example
 
Note  

The position argument passed to the Seek function is one-based, not zero-based . This means that the first byte or record is 1, not 0. For example, Seek(n, 1) positions the file pointer to the first byte or record in the file.

The macro in Listing 10 opens a file for output. A large amount of data is written to the file to give it some size . At this point, the Loc function returns 0 and EOF returns True. The Seek function is used to move the file pointer to a position in the file that allows some data to be read. The Loc function still returns 0. One hundred pieces of data are read from the file in order to advance the value returned by the Loc function. Finally, the file is deleted from the disk. Figure 4 shows information based on a file number.

click to expand
Figure 4: Information based on a file number.
Lising 10: WriteExampleGetOpenFilelnfo is found in the File module in this chapter's source code files as SC07.sxw.
start example
 Sub WriteExampleGetOpenFileInfo   Dim FileName As String              'Holds the file name   Dim n As Integer                    'Holds the file number   Dim i As Integer                    'Index variable   Dim s As String                     'Temporary string for input    FileName = ConvertToURL(CurDir) & "/delme.txt"   n = FreeFile()                                      'Next free file number   Open FileName For Output Access Read Write As #n    'Open for read/write   For i = 1 To 15032                                  'Write a lot of data     Write #n, "This is line ",CStr(i),"or",i          'Write some text   Next   Seek #n, 1022       'Move the file pointer to location 1022   For i = 1 To 100    'Read 100 pieces of data; this will set Loc     Input #n, s       'Read one piece of data into the variable s   Next   MsgBox GetOpenFileInfo(n), 0, "Infor for " & FileName   Close #n   Kill(FileName)      'Delete this file, I do not want it End Sub 
end example
 



OpenOffice.org Macros Explained
OpenOffice.org Macros Explained
ISBN: 1930919514
EAN: 2147483647
Year: 2004
Pages: 203

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net