PDA

View Full Version : Binary Files



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)

ErosOlmi
23-05-2007, 02:38
Two fast examples.
First one read chunks, cumulate into a buffer, write full buffer into output file.
Second one read and write at the same time chunks of whatever size.
Let me know if all ok. Input file is the script itself.

Example 1

uses "file"

dim InFileName as string = app_sourcename
dim OutFileName as string = app_sourcename & ".out.txt"
dim BytesToRead as long = 1024 ' Use 1K as number of bytes to read
dim BytesLeft as long = file_size(InFileName)
dim FileBuffer as string
dim MainBuffer as string

dim FileHandle as long = FILE_OPEN(InFileName, "BINARY")

'---If handle is ok
If FileHandle > 0 THEN

while %TRUE

'---Read BytesToRead bytes from input file
FileBuffer = File_GET(FileHandle, BytesToRead)

'---Cumulate to main buffer
MainBuffer += FileBuffer

'---If len of FileBuffer was not the expected one it means we are at the end of input file
if len(FileBuffer) < BytesToRead then
'---so exit loop
exit while
end if
wend

'---Close input handle
File_Close(FileHandle)

'---Save entire buffer
file_save(OutFileName, MainBuffer)

ENDIF

msgbox 0, "OK"


Example 2

uses "file"

dim InFileName as string = app_sourcename
dim OutFileName as string = app_sourcename & ".out.txt"
dim BytesToRead as long = 1024 ' Use 1K as number of bytes to read
dim BytesLeft as long = file_size(InFileName)
dim FileBuffer as string
dim MainBuffer as string

dim InFileHandle as long = FILE_OPEN(InFileName, "BINARY")

'---If handle is ok
If InFileHandle > 0 THEN

dim OutFileHandle as long = FILE_OPEN(OutFileName, "BINARY")

while %TRUE

'---Read BytesToRead bytes from input file
FileBuffer = File_GET(InFileHandle, BytesToRead)

'---Cumulate to main buffer
File_put(OutFileHandle, FileBuffer)

'---If len of FileBuffer was not the expected one it means we are at the end of input file
if len(FileBuffer) < BytesToRead then
'---so exit loop
exit while
end if
wend

'---Close input and output handle
File_Close(InFileHandle)
File_Close(OutFileHandle)

ENDIF

msgbox 0, "OK"

Michael Clease
23-05-2007, 09:04
thanks

ErosOlmi
23-05-2007, 14:20
:D a pleasure.