Link to home
Start Free TrialLog in
Avatar of Mark Brady
Mark BradyFlag for United States of America

asked on

A problem with appending a text file ?

Hi team

Hmmm, I've got a problem with my routine and can't find the error.  I have one procedure that creates a text file
if it desn't exist.  Another procedure attempts to append a string from a memo list to that file and close it.

My problem is, if I run the first procedure and the file is created (I can see this on my hard drive), when I go to run the append file function, it crashes giving me an eolerror message as there is nothing in the text file for it to append to.  Follow so far ?

Ok, if I run the first procedure, then close my application and reopen it, the second procedure works fine.  I think it's best to give you the two procedures that do this and see if you know what is wrong with it(them).

// this one cfreates the file if it doesn't exist..

Procedure TForm1.Button2Click(Sender: TObject);

label
finish;

var
addstaff, f, filename : string;


begin
addstaff := Inputbox ('Add Staff Member','Enter new name','');
if addstaff = '' then
goto finish;

filename := addstaff;
f:= ('c:\bonusplus\Data\' + filename + '.txt');

if      FileExists ( f ) = false then
        filecreate ( f );
// I can see the file on my drive after running this code. It's a blank text file //

Finish:
end;




// this one copies text in memo1 and tries to append it to the file (g)

Procedure TForm1.AddNotes(Sender: TObject);
        var
        s , i : integer;
        Templist : Tstrings;
        g: Textfile;
        f, filename : String;
begin

        Templist := TStringlist.create;
        filename := Trim(Form1.Combobox1.items[combobox1.itemindex]);

// same result as 'Adstaff' in the above procedure//

        f:= ('c:\bonusplus\Data\' + filename + '.txt');

        assignfile (g,f);
        append ( g );

Try
with Templist do
Begin

        Assign (Form2.memo1.lines);
        s:= Form2.memo1.lines.count;
 For i := 0 to s - 1 do

        WriteLn(g,Templist.strings[i]);

        Flush(g);
        Closefile(g);
 end;
Finally
        Templist.free;
end;
end;



This only works if the file exists before  run the application.

Mark
Avatar of Lukasz Zielinski
Lukasz Zielinski
Flag of Poland image

FileCreate returns handle to created file You have to release this handle with FileClose();

var FileHandle: Integer;
 
FileHandle:=FileCreate(f);
CloseFile(FileHandle);

ziolko.
Avatar of Mark Brady

ASKER

ok so I tried that but must be doing something wrong.  This is a piece oe the procedure but gives me 'incompatible types' error.

procedure TForm1.Button2Click(Sender: TObject);
label
finish;
var
addstaff, f, filename : string;
mylist : TstringList;
i: integer;
FileHandle: Integer;

begin
addstaff := Inputbox ('Add Staff Member','Enter new name','');
if addstaff = '' then
goto finish;

filename := addstaff;
f:= ('c:\bonusplus\Data\' + filename + '.txt');

if      FileExists ( f ) = false then
        FileHandle:=FileCreate(f);
        CloseFile(FileHandle);  // error here //

       

Mark
from the Delphi Help, a good place to look



The following example uses a button, a string grid, and a Save dialog box on a form. When the button is clicked, the user is prompted for a filename. When the user clicks OK, the contents of the string grid are written to the specified file. Additional information is also written to the file so that it can be read easily with the FileRead function.

procedure TForm1.Button1Click(Sender: TObject);
var
  BackupName: string;
  FileHandle: Integer;
  StringLen: Integer;
  X: Integer;
  Y: Integer;
begin
  if SaveDialog1.Execute then
  begin
    if FileExists(SaveDialog1.FileName) then
    begin
      BackupName := ExtractFileName(SaveDialog1.FileName);
      BackupName := ChangeFileExt(BackupName, '.BAK');
      if not RenameFile(SaveDialog1.FileName, BackupName) then

        raise Exception.Create('Unable to create backup file.');
    end;
    FileHandle := FileCreate(SaveDialog1.FileName);
    { Write out the number of rows and columns in the grid. }
    FileWrite(FileHandle,
      StringGrid1.ColCount, SizeOf(StringGrid1.ColCount));
    FileWrite(FileHandle,
      StringGrid1.RowCount, SizeOf(StringGrid1.RowCount));
    for X := 0 to StringGrid1.ColCount – 1 do
    begin

      for Y := 0 to StringGrid1.RowCount – 1 do
      begin
        { Write out the length of each string, followed by the string itself. }
        StringLen := Length(StringGrid1.Cells[X,Y]);
        FileWrite(FileHandle, StringLen, SizeOf(StringLen));
        FileWrite(FileHandle,
          StringGrid1.Cells[X,Y], Length(StringGrid1.Cells[X,Y]);
      end;
    end;
    FileClose(FileHandle);
  end;

end;
ASKER CERTIFIED SOLUTION
Avatar of ILE
ILE

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks ILE

I had to change it a little bit to make it work as follows;

assignfile(g,f);   // instead of   assign //
rewrite(g);
closefile(g);
end;

But it all works good now thank you.