?
Solved

Append a non-text file.

Posted on 2003-03-27
5
Medium Priority
?
149 Views
Last Modified: 2010-04-04
Is there a way to append a non-text file without copying the entire file?

I think I should use seek(filesize(f)), but with what should I open the file because I can obviously not use rewrite.
0
Comment
Question by:Stream12
[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
5 Comments
 
LVL 6

Expert Comment

by:DaFox
ID: 8220661
Hi.

Try Reset() or even better use TFileStream:

var
  Stream1: TFileStream;
  c: char;
begin
  Stream1 := TFileStream.Create('filename', fmOpenRead);
  Stream1.Seek(1, soFromEnd);
  try
    while (Stream1.Position > 0) do
    begin
      Stream1.Read(c, SizeOf(c));
      if (Stream1.Position := 1) then break;
      Stream1.Seek(2, soFromCurrent);
    end;
  finally
    Stream1.Free;
  end;
end;

(not tested)

Markus
0
 
LVL 3

Expert Comment

by:emadat
ID: 8221128
Try This. It works for me all the time; and even faster than TFileStream.

//**************************************************
var
     F: File;
begin
     AssignFile(F, _File_Name_);
     if FileExists(_File_Name_) then begin
          reset(F, 1);
          seek(F, FileSize(F));
     end else
          ReWrite(F, 1);
     BlockWrite(F, _Data_Buffer_, SizeOf(_Data_Buffer_));
     CloseFile(F);
end;
//**************************************************

Emad
0
 

Expert Comment

by:mvieira
ID: 8221943
This code will append a binary file with any data (I used a PChar for data, but you can use something else)


uses SysUtils;

procedure AppendDataToFile(Data:PChar);

var FHandle,TryNum,FSize:Integer;
    S:String;
    t:Integer;
    Filename:String;

begin
  Filename:='c:\test.bin';
  FHandle:=-1;
  if (NOT FileExists(Filename)) then
    FHandle:=FileCreate(Filename)  // file does not exists: create
  else
    FHandle:=FileOpen(Filename, fmOpenWrite or fmShareDenyWrite);  // file exists: open it
  if FHandle<=0 then
    raise Exception.Create('Could not open file');
  try
    FSize:=FileSeek(FHandle,0,2);
    if FileWrite(FHandle,Data^,StrLen(Data))<=0 then
      raise Exception.Create('Error writing file');
  finally
    FileClose(FHandle);
  end;
end;
0
 
LVL 3

Accepted Solution

by:
LukA_YJK earned 150 total points
ID: 8223440
Using Reset does not mean that you can only read it. You can even write to it. Try it, hope it will work.

Reset(F);
Seek(F, FileSize(F));
Write(F,d);
Close(F);
0
 

Author Comment

by:Stream12
ID: 8293196
It works thanks, for some reason I thought reset means readonly.

Sorry I took so long.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

800 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