The CRBasic help for FileRead states that it can be used for reading into an array!
How do I do that?
And it also state that it will read to the end of file.
What should I use in lenght parameter to read to the end of file ? (-1 or 0 or ?)
regards
morten
Can you tell us more about your application? I would like to know more about what you want to accomplish.
But to answer your questions:
- How do I do that?
a) FileOpen to open the file
b) FileRead to read the file, specifying the destination as an array and the number of bytes to read
c) FileClose
- What should the length parameter be?
It should be equal to the maximum number of bytes you wish to read from the file starting at the seek point specified during FileOpen().
Beginning at the seek point specified during FileOpen(), FileRead() will read in the number of bytes you specified or until it reaches the end of file (EOF).
It will place those bytes into the destination variable.
Each value in an array of Float or Long is 4 bytes. If the destination is an array of Float or Long, the first four bytes read in will be placed into Array(1), the next four bytes into Array(2), and so on.
If the destination was an array of "String * 15", the first 16 bytes read would be placed into Array(1), the next 16 into Array(2), and so on.
A practical application is reading in multiple floating point numbers stored in a "binary file". I have included an example below that writes an example file and then reads it back in.
I suppose you could also do this with a file of ASCII / string data but you would want each value or line to be fixed width.
Public DataWrite(5) = {1.23,45.6,789.1,11.12,131.4}
Public FileHandle1
Public BytesWritten
Public CloseResult1
Public FileHandle2
Public DataRead(5)
Public BytesRead
Public CloseResult2
BeginProg
SetStatus("USRDriveSize",1000000)
FileHandle1 = FileOpen ("USR:data.txt","wb",-1)
BytesWritten = FileWrite (FileHandle1,DataWrite(),20)
CloseResult1 = FileClose (FileHandle1)
FileHandle2 = FileOpen ("USR:data.txt","rb",-1)
BytesRead = FileRead (FileHandle2,DataRead(),20)
CloseResult2 = FileClose (FileHandle2)
EndProg
Thanks Sam
Your example is descriping my needs.
The help is missing what you just wrote.
morten
Thanks for the feedback, Morten.
I'll work with the Software Support Group to improve the help text for FileRead.
Happy programming.