Solved

Appending a block to a file

Posted on 1998-06-23
5
146 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

688 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