| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <html>
- <head>
- <title>Blitz3D Docs</title>
- <link rel=stylesheet href=../css/commands.css type=text/css>
- </head>
- <body>
- <h1>OpenFile (filename$)</h1>
- <h1>Parameters</h1>
- <table>
- <tr>
- <td>
- filename$ = any valid path and filename. The returned value is the filehandle which is used by other file handling commands.
- </td>
- </tr>
- </table>
- <h1>Description</h1>
- <table>
- <tr>
- <td>
- This command opens the designated file and prepares it to be updated. The file must exists since this function will not create a new 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.
<br />
-
<br />
- The file handle that is returned is an integer value that the operating system uses to identify which file is to be read and written to and must be passed to the functions such as ReadInt() and WriteInt().
<br />
-
<br />
- Note extreme care needs to be exercised when updating files that contain strings since these are not fixed in length.
- <br>
- <br>
- See also: <a class=small href=ReadFile.htm>ReadFile</a>, <a class=small href=WriteFile.htm>WriteFile</a>, <a class=small href=CloseFile.htm>CloseFile</a>, <a class=small href=SeekFile.htm>SeekFile</a>.
- </td>
- </tr>
- </table>
- <h1><a href=../2d_examples/OpenFile.bb>Example</a></h1>
- <table>
- <tr>
- <td>
- ; Changing part of a file using OpenFile, SeekFile and WriteInt
<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 />
- ; Open the file and change the Third Integer
<br />
-
<br />
- file = OpenFile("mydata.dat")
<br />
- SeekFile( file, 8 ) ; Move to the third integer in the file
<br />
- WriteInt( file, 9999 ) ; Replace the original value with 9999
<br />
- CloseFile( file )
<br />
-
<br />
- DisplayFile( "The file after being midified", "mydata.dat" )
<br />
- WaitKey()
<br />
- ; **** Function Definitions follow ****
<br />
-
<br />
- ; Read the file and print it
<br />
- Function DisplayFile( Tittle$, Filename$ )
<br />
- Print tittle$
<br />
- filein = ReadFile( Filename$ )
<br />
- While Not Eof( filein )
<br />
- Number = ReadInt( filein )
<br />
- Print Number
<br />
- Wend
<br />
- CloseFile( filein )
<br />
- Print
<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=OpenFile&ref=comments target=_blank>here</a> to view the latest version of this page online</body>
- </html>
|