Solved

file writing without overwriting

Posted on 2002-04-23
8
178 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
[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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

729 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