| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <html>
- <head>
- <title>Blitz3D Docs</title>
- <link rel=stylesheet href=../css/commands.css type=text/css>
- </head>
- <body>
- <h1>FilePos (filehandle)</h1>
- <h1>Parameters</h1>
- <table>
- <tr>
- <td>
- filehandle = the variable returned by the Readfile WriteFile or OpenFile when the file was opened. The value returned is the offset from the start of the file. ( 0 = Start of the file )
- </td>
- </tr>
- </table>
- <h1>Description</h1>
- <table>
- <tr>
- <td>
- This command returns the current position within a file that is being processed following ReadFile, WriteFile or OpenFile. The returned integer is the offsets in bytes from the start of the file to the current read/write position. Note, this is zero when pointing to the first byte of the file.
<br />
-
<br />
- By using FilePos and SeekFile the position within the file that is being read or written can be determined and also changed. This allows a file to be read and updated without having to make a new copy of the file or working through the whole file sequentially. This could be useful if you have created a database file and you want to find and update just a few records within it. It is also possible to create an index file that contains pointers to where each record starts in a data file.
- </td>
- </tr>
- </table>
- <h1><a href=../2d_examples/FilePos.bb>Example</a></h1>
- <table>
- <tr>
- <td>
- ; Changing part of a file using OpenFile, SeekFile, FilePos
<br />
- ; note FilePos is used in the SearchFile function at the end of this example
<br />
-
<br />
- ; Open/create a file to Write
<br />
- fileout = WriteFile("mydata.dat")
<br />
-
<br />
- ; Write the information to the file
<br />
- WriteInt( fileout, 1 )
<br />
- WriteInt( fileout, 2 )
<br />
- WriteInt( fileout, 3 )
<br />
- WriteInt( fileout, 4 )
<br />
- WriteInt( fileout, 5 )
<br />
-
<br />
- ; Close the file
<br />
- CloseFile( fileout )
<br />
-
<br />
- DisplayFile( "The file as originally written", mydata.dat" )
<br />
- Position = SearchFile( 4 , "mydata.dat" )
<br />
- Write "Value 4 was found "
<br />
- Write Position
<br />
- Print " bytes from the start."
<br />
- Print
<br />
-
<br />
- ; Open the file and change the value 3 to 9999
<br />
-
<br />
- file = OpenFile("mydata.dat")
<br />
- SeekFile( file, Position ) ; Move to the found location
<br />
- WriteInt( file, 9999 ) ; Replace the original value with 9999
<br />
- CloseFile( file )
<br />
-
<br />
-
<br />
- DisplayFile( "The file after being modified", "mydata.dat" )
<br />
- WaitKey()
<br />
- End ; End of program
<br />
-
<br />
- ; **** Function Definitions follow ****
<br />
-
<br />
- ; Read the file and print it
<br />
- Function DisplayFile( Tittle$, Filename$ )
<br />
- Print tittle$
<br />
- file = ReadFile( Filename$ )
<br />
- While Not Eof( file )
<br />
- Number = ReadInt( file )
<br />
- Print Number
<br />
- Wend
<br />
- CloseFile( file )
<br />
- Print
<br />
- End Function
<br />
-
<br />
- ; Search a file of integers for the Wanted data value
<br />
- ; Note the need to subtract 4 from the location since having read it
<br />
- ; we are now pointing at the next Integer also Return() was placed
<br />
- ; after closing the file so it is closed properly
<br />
- Function SearchFile( Wanted, Filename$ )
<br />
- file = ReadFile( Filename$ )
<br />
- While Not Eof( file )
<br />
- If ReadInt( file ) = Wanted Then Location = FilePos( file ) - 4
<br />
- Wend
<br />
- CloseFile( file )
<br />
- Return( Location )
<br />
- End Function
- </td>
- </tr>
- </table>
- <br>
- <a target=_top href=../index.htm>Index</a><br>
- <br>
- Click <a href=http://www.blitzbasic.co.nz/b3ddocs/command.php?name=FilePos&ref=comments target=_blank>here</a> to view the latest version of this page online</body>
- </html>
|