OpenNewFile
ChannelIndex = OpenNewFile(Filename$)
 
Parameters:

    Filename$ = The name of the file you wish to open file access on
Returns:

    ChannelIndex = Index of the file channel to use
 

     OpenNewFile opens a file for combined read & write access. The command only requires the filename. OpenNewFile will return the file channel index to you. The file channel is then used subsequent read/writing commands (ie ReadByte, ReadWord, ReadInt,WriteByte, WriteWord, WriteInt etc etc.


     Files can be accessed both sequentially and randomly.


Sequential Access

      Sequential file access is where you step through the file in order, in other words, you begin reading at the start of the file, and continue reading/writing through it from start to finish. Never stepping backward or skipping forward.


Random Access

      Random file access, is where you jump around the file reading one section, then perhaps jumping back or skipping forward. So your randomly accessing parts of file at will. This is often invaluable when you need to read data sections found in image files, or constructing your own pack file format.




FACTS:


      * When Reading data from a file, the File Must exist in order to be read. If you're just wanting to read data from the file, then we recommend using ReadFile or ReadNewFile - Same applies for just writing data to a file. For that see WriteFile or WriteNewFile

      * Files must exist before it can written to. See-> MakeFile





Mini Tutorial:


      This example creates a simple file with some various bits of data stored within it, it then reads this data back again.


  
  File$=TempDir$()+"PB_TestFile.txt"
  
; CReate a File for writing data to
  MyFile=WriteNewFile(File$)
  
; Write a Byte to file channel 1, of value 200
  WriteByte MyFile,200
  
; Write a Word to file channel 1, of value 64000
  WriteWord MyFile,64000
  
; Write a INTEGER to file channel 1, of value 123456
  WriteInt MyFile,123456
  
; Write a FLOAT to file channel 1, of value 123.456
  WriteFloat MyFile,123.456
  
; Write a String to file channel 1, of value Hello World
  WriteString MyFile,"Hello Word"
  
; Close this file channel
  CloseFile MyFile
  
  
  Print "============================"
  Print "Reading File:"+File$
  Print "============================"
  
  
; Open a file to READ with OpenNEwFile
  MyFile=OpenNewFile(File$)
  
; Read and display the byte value
  Print "   Byte Data:"+Str$(ReadByte(MyFile))
  
; Read and display the word value
  Print "   Word Data:"+Str$(ReadWord(MyFile))
  
; Read and display the Integer value
  Print "Integer Data:"+Str$(ReadInt(MyFile))
  
; Read and display the Float value
  Print "  Float Data:"+Str$(ReadFloat(MyFile))
  
; Read and display the String value
  Print "  String Data:"+ReadString$(MyFile)
  
; Close this file channel
  CloseFile MyFile
  
  
  If FileExist(file$) Then DeleteFile file$
  
; Display the screen and wait for a key press
  Sync
  WaitKey
  
  



This example would output.

  
  ============================
  Reading File:C:Windows\temp\Pb_TestFile.txt
  ============================
  Byte Data:200
  Word Data:64000
  Integer Data:123456
  Float Data:123.456
  String Data:Hello World
  

 
Related Info: CloseFile | FilePos | FileSize | OpenFile | ReadByte | ReadByteAt | ReadChr$ | ReadChrAt$ | ReadFile | ReadFloat | ReadFloatAt | ReadInt | ReadIntAt | ReadString$ | ReadStringAt$ | ReadValue | ReadValue# | ReadWord | ReadWordAt | WriteFile | WriteFloat | WriteFloatAt | WriteInt | WriteIntAt | WriteString | WriteStringAt | WriteWord | WriteWordAt :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com