Reading a file into a string?

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.
using string for binary stuffs is bad.
What my app needs to do is load a big binary file (*.exe of 136 K), change some bytes of it, then rewrite it back to disk. What's the best way to do this?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by ford escort.

load the file in a memory bank then pokeb() to the offset you want to change bytes ans saveback the memorybank to the file ...

fil$="myfile.bin"; here's the filename
file=openfile(1,fil$);open the file
usefile(1);and use it
size=lof();calculate the lenght of the file
*buffer=AllocateMemoryBank(1,size,0): reserve memory for the file
ReadMemory(*Buffer,size) ;read the file to memory
pokeb(*buffer+25,00); change the byte 25 of the file
pokeb(*buffer+50,255); change the byte 50 of the file
fileseek(0);return to the start of the file
writememory(*buffer,size);writeback the file to the disk
closefile(1);close the file
freememorybank(1);free the memory for future use
end;quit the program

i hope this help (i'm not sure that using fileseek's a good method i don't try this code)

42...42...42...42...42...42...42...42...
don't forget a plastic bag and a towel
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.

A few tips for you Ford!

Always check that the file is properly opened i.e:
if ReadFile(1,"binfile.bin")
...
if OpenFile(1,"otherfile.bin")
...
Failing to check this will cause crashes.

When using the OpenFile() command be aware that if the specified file doesn't exist it will be created.

Taking this into account your example will look like this:

Code: Select all

fil$="myfile.bin"; here's the filename
if ReadFile(1,fil$) ;open the file for reading
  size=lof() ;calculate the lenght of the file
  *buffer=AllocateMemoryBank(1,size,0): reserve memory for the file
  ReadMemory(*Buffer,size) ;read the file to memory
  pokeb(*buffer+25,00); change the byte 25 of the file
  pokeb(*buffer+50,255); change the byte 50 of the file
  CloseFile(1) ; Close the file
  If OpenFile(1,fil$+"new") ; Open a file for writing using a new name, just in case;)
    writememory(*buffer,size);writeback the file to the disk
    closefile(1);close the file
  endif
endif
end;quit the program and automatically return the reserved memory
/Pupil
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.

Okay here is an example, that works !
(Read in a Picture and scramble the colors)

fil$="c:\temp.bmp"; here's the filename
If ReadFile(1,fil$) ;open the file for reading
size=lof() ;calculate the lenght of the file
MessageRequester("Information", "Find File:" + fil$+" with Size: " +str(size) ,0)
If size>0
*buffer=AllocateMemoryBank(1,size,0); reserve memory For the file
;MessageRequester("Information", "*Buffer:" + str(*buffer) ,0)
ReadMemory(*buffer,size) ;read the file to memory
For T=300 To size Step 3
Color=peekL(*buffer+T)
pokeb(*buffer+T,BLUE(GR));
pokeb(*buffer+T+1,BLUE(GR));
pokeb(*buffer+T+1,BLUE(GR));

Next T
EndIf
CloseFile(1) ; Close the file
If OpenFile(1,"c:\temp1.bmp") ; Open a file for writing using a new name, just in case;)
writememory(*buffer,size);writeback the file to the disk
closefile(1);close the file
MessageRequester("Information", "DONE" ,0)
EndIf
Else
MessageRequester("Information", "File:" + fil$+" not found !" ,0)
EndIf
End


Siggi
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by sarabob.

OK - but what if I now want to take an exe file and replace 0x0A,0x0D with just 0x0A (ie replace CRLF with LF - windows to unix/amiga line endings) - this is easy with a string, not so easy with a memory block.

I haven't tried this, but looking at previous posts, could you just do
a$=space(4000)
a$=a$+space(4000)
a$=a$+space(4000)
a$=a$+space(4000)
a$=a$+space(4000)

and then play with a$?

This seems like a trivial problem for a modern programming language, and I'm not entirely convinced at the "it's an Amiga problem" argument, considering that other Amiga languages don't have this problem...

--
Adam
Post Reply