Solved

file writing without overwriting

Posted on 2002-04-23
8
175 Views
Last Modified: 2010-04-04

How can i insert text from multiple files in one file? The file where i would want to write to doesnt have an extension.
My problem is that I want to insert data in a file that already contains data. when i do something like this:

try
    // source file
    assignfile(sourcefile,'C:\Documents and     Settings\Administrator\Desktop\source');
    reset(sourcefile);
    // destination file
    Rewrite(destination,'c:\destination);
    append(destination);
    pointer := eof(destination);
    while not eof(source') do
    begin
      ReadLn( source', line);
      writeln(destination,line);
    end;
  finally
    CloseFile(source');
    CloseFile(destination);
  end;

when i run this multiple times, it overwrites data in the destination file. that sux.
0
Comment
Question by:peterjoustra
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 6

Expert Comment

by:DrDelphi
ID: 6962642
Load the file into a TstringList and the new lines with the Add method. Then save to file. Viola!
 Behold:

var list:TstringList;
begin
     list:=Tstringlist.create;
     List.LoadFromFile('Z:\myfile');
     List.add('this is a new line '+TimeTostr(now));
     List.add('So is this one '+TimeTostr(now));
     List.SaveToFile('Z:\myfile');
     FreeAndNil(list);
end;



Good luck!!

 
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6962645
>Rewrite(destination,'c:\destination);
allways creates a new file, do a check for existence before using rewrite , if the file not exists, otherwise use reset

look at fileexists-function

meikl ;-)
   
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6962647
or do as drdelphi suggested
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 17

Accepted Solution

by:
geobul earned 200 total points
ID: 6962699
Hi,

procedure AppendSecondFile(Source, Dest: string);
var
 NewFile: TFileStream;
 OldFile: TFileStream;
begin
 NewFile:=TFileStream.Create(Dest, fmOpenReadWrite);
 NewFile.Seek(0,soFromEnd);
 OldFile:=TFileStream.Create(Source, fmShareDenyNone);
 NewFile.CopyFrom(OldFile, OldFile.Size);
 FreeAndNil(OldFile);
 FreeAndNil(NewFile);
end;

Usage:
// add 'c:\test2' to 'c:\temp\test1'
procedure TForm1.Button1Click(Sender: TObject);
begin
  AppendSecondFile('c:\test2','c:\temp\test1');
end;

Regards, Geo
0
 
LVL 17

Expert Comment

by:geobul
ID: 7169115
Why does this question have 'Pending delete' status?
0
 
LVL 1

Expert Comment

by:pnh73
ID: 9004030
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept answer from DrDelphi

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Paul (pnh73)
EE Cleanup Volunteer
0
 
LVL 17

Expert Comment

by:geobul
ID: 9004286
Well, I have to disagree again. DrDelphi's solution doesn't add one file at the end of another. It could be modified to do so, of course.

Regards, Geo
0
 
LVL 1

Expert Comment

by:pnh73
ID: 9004893
Objection considered.

I will leave a recommendation in the Cleanup topic area that this question is:

Accept answer from geobul

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Paul (pnh73)
EE Cleanup Volunteer
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

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…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 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