Author Topic: Read a file to a buffer  (Read 4743 times)

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10725
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Read a file to a buffer
« on: 2009-Nov-19 »
This code reads the notepad.exe to a string buffer and write it in the .app directory to show it worked.

Code: (glbasic) [Select]
LOCAL buffer$
ReadFile("C:/Windows/notepad.exe", buffer$)

// test if it works
OPENFILE(1, "copypad.exe", 0)
WRITESTR 1, buffer$
CLOSEFILE 1





FUNCTION ReadFile: filename$, BYREF buffer$
LOCAL file%=GENFILE()
LOCAL fs% = GETFILESIZE(filename$)
buffer$=""
IF OPENFILE(file%, filename$, 1)
LOCAL i%
LOCAL ndword% = fs%/4 - 1
// read 4 bytes at once (a tad faster)
FOR i% = 0 TO ndword%
LOCAL b%
READLONG file%, b%
INC buffer$, CHR$(bAND(    b%     , 0xff))
INC buffer$, CHR$(bAND(ASR(b%,  8), 0xff))
INC buffer$, CHR$(bAND(ASR(b%, 16), 0xff))
INC buffer$, CHR$(bAND(ASR(b%, 24), 0xff))
NEXT
DEC fs, 4*ndword

FOR i% = 0 TO fs%-1
LOCAL b%
READBYTE file%, b%
INC buffer$, CHR$(b%)
NEXT
CLOSEFILE file%
ENDIF
RETURN LEN(buffer$)
ENDFUNCTION

MrTAToad

  • Guest
Re: Read a file to a buffer
« Reply #1 on: 2009-Nov-20 »
The only real problem is that it cant cope with sizes less than 4 bytes :)

Offline FutureCow

  • HelpEditor
  • Prof. Inline
  • ******
  • Posts: 680
    • View Profile
Re: Read a file to a buffer
« Reply #2 on: 2009-Nov-20 »
I found a couple of minor mistakes in the function so I've fixed them and made it so it can cope with sizes < 4 bytes

Corrections were made to these lines
Code: (glbasic) [Select]
LOCAL ndword% = fs%/4 - 1
to
LOCAL ndword% = fs%/4

FOR i% = 0 TO ndword%
to
FOR i% = 0 TO ndword%-1

Code: (glbasic) [Select]
LOCAL buffer$
ReadFile("C:/Windows/notepad.exe", buffer$)

// test if it works
OPENFILE(1, "copypad.exe", 0)
WRITESTR 1, buffer$
CLOSEFILE 1

FUNCTION ReadFile: filename$, BYREF buffer$
LOCAL file%=GENFILE()
LOCAL fs% = GETFILESIZE(filename$)

buffer$=""
IF OPENFILE(file%, filename$, 1)
LOCAL i%
IF fs > 3
LOCAL ndword% = fs%/4
// read 4 bytes at once (a tad faster)
FOR i% = 0 TO ndword%-1
LOCAL b%
READLONG file%, b%
INC buffer$, CHR$(bAND(    b%     , 0xff))
INC buffer$, CHR$(bAND(ASR(b%,  8), 0xff))
INC buffer$, CHR$(bAND(ASR(b%, 16), 0xff))
INC buffer$, CHR$(bAND(ASR(b%, 24), 0xff))
NEXT
DEC fs%, 4*ndword
ENDIF
FOR i% = 0 TO fs%-1
LOCAL b%
READBYTE file%, b%
INC buffer$, CHR$(b%)
NEXT
CLOSEFILE file%
ENDIF
RETURN LEN(buffer$)
ENDFUNCTION
« Last Edit: 2009-Nov-20 by FutureCow »

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10725
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: Read a file to a buffer
« Reply #3 on: 2009-Nov-20 »
oops ;)

Offline FutureCow

  • HelpEditor
  • Prof. Inline
  • ******
  • Posts: 680
    • View Profile
Re: Read a file to a buffer
« Reply #4 on: 2009-Nov-20 »
 :enc: It's not often I find a fault in something you've written Gernot! *laugh*

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10725
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: Read a file to a buffer
« Reply #5 on: 2009-Nov-20 »
You do find a lot of my bugs. Unfortunately :P

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1781
    • View Profile
    • Homepage
Re: Read a file to a buffer
« Reply #6 on: 2009-Nov-20 »
Better find bugs early than late :-)