Solved

Appending a block to a file

Posted on 1998-06-23
5
142 Views
Last Modified: 2010-05-18
I have developed a component to read an rpt (ReportSmith) file, and change the hard-coded paths within it so that my report points to the correct set of data.  

It works perfectly, but I am reading it in as a File of Byte and writing it back to a new file, and all this takes a long time if it is a big file, especially if there are graphics within the Report.

I have discovered that I don't have to go through the whole file - just the first bit of it which has the embedded paths.

My question is - how can I stop reading in byte by byte when I have done the changes and just append the rest of the original file from that point.  (OR if I were to block read the first part into memory, how would I read it and manipulate it in memory and then write it back).

0
Comment
Question by:jdthedj
  • 3
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
vladika earned 150 total points
ID: 1355358
Use class TFileStream

for example
procedure TForm1.Button1Click(Sender: TObject);
var FileStream: TFileStream;
    S: string;
begin
  FileStream := TFileStream.Create('c:\test.txt', fmOpenReadWrite);
  try
    FileStream.Position := 4;  // set offset into the stream for reading and writing
    SetString(S, nil, 4);  // allocate space for S (for example 4 bytes)
    FileStream.Read(PChar(S)^, 4); // read 4 bytes from filestream into S
    if S = '1234' then S := '4321' else S := '1234';  // correct S
    FileStream.Position := 4;  // set old position
    FileStream.Write(PChar(S)^, 4);  // write correct S into file stream
  finally
    FileStream.Free;  // the end
  end;
end;


0
 
LVL 3

Expert Comment

by:vladika
ID: 1355359
If you want test my procedure then create manually file c:\test.txt
0
 
LVL 3

Author Comment

by:jdthedj
ID: 1355360
Hi Valdika - Thanks for your help

Sorry  - but I forgot to mention I am using D1 and WFW 3.11, so I haven't got the SetString command.

I'm not sure if reading the data as strings would work because a lot of the bytes are outside the normal character range.

I am happy to read the first part in byte by byte as I have been doing, because the data to be read back may have added bytes or fewer bytes.  I want to append the whole of the rest of the file in one hit if possible, or in large chunks after fixing up the first part of the file.


0
 
LVL 3

Expert Comment

by:vladika
ID: 1355361
Hi Jdthedj

1) You may use not only string. Read, Write, ReadBuffer, WriteBuffer methods
works with ANY BUFFER, I use string as buffer only for example.

2) To copy the rest of one stream into other stream use CopyFrom method
Here is example

procedure TForm1.AnalizeAndReplace(SrcName, DstName: string);
var SrcStream, DstStream: TFileStream;
    B: Byte;
    S: string;
begin
  SrcStream := TFileStream.Create(SrcName, fmOpenRead); { Source file stream }
  try
    DstStream := TFileStream.Create(DstName, fmCreate); { Dest file stream }
    try
      while SrcStream.Position < SrcStream.Size do { while not end of file }
      begin
        SrcStream.Read(B, 1);  { read one Byte }
        { your code for analize B
          for example search Char(B) = 'A'
          when found insert into DstStream 'I found' and
          copy rest }
        if Char(B) = 'A' then
        begin
          S := 'I found';
          DstStream.Write(S[1], Length(S)); { write S into Stream, I use string as buffer only for example}
          Break; { exit from while }
        end;
        DstStream.Write(B, 1);  {copy B into DstStream }
      end;
      { copy rest }
      if SrcStream.Position < SrcStream.Size then
        DstStream.CopyFrom(SrcStream, SrcStream.Size - SrcStream.Position);
    finally
      DstStream.Free;
    end;
  finally
    SrcStream.Free;
  end;
end;

{ How to use }
procedure TForm1.Button1Click(Sender: TObject);
begin
  AnalizeAndReplace('c:\src.dat', 'c:\dst.dat'); { src file name and dest file name }
end;


0
 
LVL 3

Author Comment

by:jdthedj
ID: 1355362
Hi Vladika

Thanks a lot for all your help.  You have explained it very clearly.  It is a lot easier to program when you can understand what you are doing!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to convert wav to mp3 in delphi 9 198
how to center only a line in richedit? 4 56
Delphi 2 59
select query - oracle 16 100
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question