Board index » delphi » Reading a non-text file

Reading a non-text file

Hi,
I've never really understood how to read a non-text file in an efficient way
in delphi, so I'd be very grateful if someone could help me out here. I need
to open a non-text file (similar to an .exe file, in that most of it is
non-sensical, but it has some strings in it), and modify small bits of it,
like I need to change every occurance of

"Message 104 'random error text here'"
to a different string, and then write a new file which is identical to the
original except with these 'Message' strings changed. The problem I had when
trying to read the file as a text file is that it contains other characters,
such as #0 or something, which makes Delphi think it has reached the end of
the file. All help appreciated!

Thanks,
James.

 

Re:Reading a non-text file


Look at TFileStream or THandleStream
Quote
On Wed, 30 May 2001 09:44:18 +0100, "James" <nos...@nospam.com> wrote:
>Hi,
>I've never really understood how to read a non-text file in an efficient way
>in delphi, so I'd be very grateful if someone could help me out here. I need
>to open a non-text file (similar to an .exe file, in that most of it is
>non-sensical, but it has some strings in it), and modify small bits of it,
>like I need to change every occurance of

>"Message 104 'random error text here'"
>to a different string, and then write a new file which is identical to the
>original except with these 'Message' strings changed. The problem I had when
>trying to read the file as a text file is that it contains other characters,
>such as #0 or something, which makes Delphi think it has reached the end of
>the file. All help appreciated!

>Thanks,
>James.

Re:Reading a non-text file


Quote
"James" <nos...@nospam.com> wrote in message

news:9f2bsf$7sp$1@neptunium.btinternet.com...

Quote
> Hi,
> I've never really understood how to read a non-text file in an efficient
way
> in delphi, so I'd be very grateful if someone could help me out here. I
need
> to open a non-text file (similar to an .exe file, in that most of it is
> non-sensical, but it has some strings in it), and modify small bits of it,
> like I need to change every occurance of

> "Message 104 'random error text here'"
> to a different string, and then write a new file which is identical to the
> original except with these 'Message' strings changed. The problem I had
when
> trying to read the file as a text file is that it contains other
characters,
> such as #0 or something, which makes Delphi think it has reached the end
of
> the file. All help appreciated!

Without knowing the structure of the file it will be a tricky and
potentially dangerous task. Reading past #0 is relatively trivial, use a
stream. However if you don't know the format of the file you are working
with you will probably spend an awful lot of time on the task with little
hope of real success.

In my experience many files of the type you describe work with fixed length
strings or store the length of the string before storing the string. In the
later case the length isn't necessarily written just before the string. In
both these cases you can replace the strings with ones of exactly the same
length. The other format for storing strings is to terminate them with #0.
In this later case you may be out of luck, you can try padding shorter
replacement strings with #0, and that may work. Of course, all of this goes
out the window if the file format contains any kind of checksum or if you
need to write strings longer than what is currently in the file.

Other Threads