FilePos.htm 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <html>
  2. <head>
  3. <title>Blitz3D Docs</title>
  4. <link rel=stylesheet href=../css/commands.css type=text/css>
  5. </head>
  6. <body>
  7. <h1>FilePos (filehandle)</h1>
  8. <h1>Parameters</h1>
  9. <table>
  10. <tr>
  11. <td>
  12. 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 )
  13. </td>
  14. </tr>
  15. </table>
  16. <h1>Description</h1>
  17. <table>
  18. <tr>
  19. <td>
  20. 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 />
  21. <br />
  22. 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.
  23. </td>
  24. </tr>
  25. </table>
  26. <h1><a href=../2d_examples/FilePos.bb>Example</a></h1>
  27. <table>
  28. <tr>
  29. <td>
  30. ; Changing part of a file using OpenFile, SeekFile, FilePos <br />
  31. ; note FilePos is used in the SearchFile function at the end of this example <br />
  32. <br />
  33. ; Open/create a file to Write <br />
  34. fileout = WriteFile("mydata.dat") <br />
  35. <br />
  36. ; Write the information to the file <br />
  37. WriteInt( fileout, 1 ) <br />
  38. WriteInt( fileout, 2 ) <br />
  39. WriteInt( fileout, 3 ) <br />
  40. WriteInt( fileout, 4 ) <br />
  41. WriteInt( fileout, 5 ) <br />
  42. <br />
  43. ; Close the file <br />
  44. CloseFile( fileout ) <br />
  45. <br />
  46. DisplayFile( "The file as originally written", mydata.dat" ) <br />
  47. Position = SearchFile( 4 , "mydata.dat" ) <br />
  48. Write "Value 4 was found " <br />
  49. Write Position <br />
  50. Print " bytes from the start." <br />
  51. Print <br />
  52. <br />
  53. ; Open the file and change the value 3 to 9999 <br />
  54. <br />
  55. file = OpenFile("mydata.dat") <br />
  56. SeekFile( file, Position ) ; Move to the found location <br />
  57. WriteInt( file, 9999 ) ; Replace the original value with 9999 <br />
  58. CloseFile( file ) <br />
  59. <br />
  60. <br />
  61. DisplayFile( "The file after being modified", "mydata.dat" ) <br />
  62. WaitKey() <br />
  63. End ; End of program <br />
  64. <br />
  65. ; **** Function Definitions follow **** <br />
  66. <br />
  67. ; Read the file and print it <br />
  68. Function DisplayFile( Tittle$, Filename$ ) <br />
  69. Print tittle$ <br />
  70. file = ReadFile( Filename$ ) <br />
  71. While Not Eof( file ) <br />
  72. Number = ReadInt( file ) <br />
  73. Print Number <br />
  74. Wend <br />
  75. CloseFile( file ) <br />
  76. Print <br />
  77. End Function <br />
  78. <br />
  79. ; Search a file of integers for the Wanted data value <br />
  80. ; Note the need to subtract 4 from the location since having read it <br />
  81. ; we are now pointing at the next Integer also Return() was placed <br />
  82. ; after closing the file so it is closed properly <br />
  83. Function SearchFile( Wanted, Filename$ ) <br />
  84. file = ReadFile( Filename$ ) <br />
  85. While Not Eof( file ) <br />
  86. If ReadInt( file ) = Wanted Then Location = FilePos( file ) - 4 <br />
  87. Wend <br />
  88. CloseFile( file ) <br />
  89. Return( Location ) <br />
  90. End Function
  91. </td>
  92. </tr>
  93. </table>
  94. <br>
  95. <a target=_top href=../index.htm>Index</a><br>
  96. <br>
  97. 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>
  98. </html>