Michael Clease
23-05-2007, 00:18
I just cant seem to get reading and writing to binary files to work. I have a text file of 6 bytes 74 65 73 74 0d 0a or TEST $CRLF. I read the file in and write the same buffer out and it doesnt work, can someone explain why? I really wanted to read a chunk (1KB or 1MB) of a file then read each byte and use its value as an index to an array 0 - 255 (+1).
Heres some test code i made seperate to my main app just to try an work out the file handling.
FileHandle = FILE_OPEN("C:\a\myTest.txt", "BINARY")
If FileHandle >0 THEN
BytesToRead = 1024 ' Use 1K as number of bytes to read
BytesLeft = FileLength(FileNum) ' Get Current FileLength
For n = 1 to FileLength(FileNum)
if BytesLeft < 1024 then ' Is whats left to read < 1k
BytesToRead = BytesLeft ' Set new read size
ELSE
FileBuffer = File_GET(FileHandle, BytesToRead)
BytesLeft -= 1024
ENDIF
Next
FileERR = FILE_CLOSE(FileHandle)
Else
MSGBOX 0, "Error Opening File "+TheFileList(FileNum)
ENDIF
FileHandle = FILE_OPEN("C:\a\TesterFile.txt", "BINARY")
FileERR = File_Put(FileHandle,FileBuffer+$Crlf) ' if the +$crlf is not here it creates an empty file.
Fileerr = File_Close(FileHandle)
Heres some test code i made seperate to my main app just to try an work out the file handling.
FileHandle = FILE_OPEN("C:\a\myTest.txt", "BINARY")
If FileHandle >0 THEN
BytesToRead = 1024 ' Use 1K as number of bytes to read
BytesLeft = FileLength(FileNum) ' Get Current FileLength
For n = 1 to FileLength(FileNum)
if BytesLeft < 1024 then ' Is whats left to read < 1k
BytesToRead = BytesLeft ' Set new read size
ELSE
FileBuffer = File_GET(FileHandle, BytesToRead)
BytesLeft -= 1024
ENDIF
Next
FileERR = FILE_CLOSE(FileHandle)
Else
MSGBOX 0, "Error Opening File "+TheFileList(FileNum)
ENDIF
FileHandle = FILE_OPEN("C:\a\TesterFile.txt", "BINARY")
FileERR = File_Put(FileHandle,FileBuffer+$Crlf) ' if the +$crlf is not here it creates an empty file.
Fileerr = File_Close(FileHandle)