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
LVL 20
Mark BradyPrincipal Data EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

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

ziolko.
0
Mark BradyPrincipal Data EngineerAuthor Commented:
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
0
Slick812Commented:
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;
0
ILECommented:
instead of use file create use

var g:textfile;


supstitute filecreate('name')  with


begin
assign('name',g);
rewrite(g);
closefile(g);
end;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mark BradyPrincipal Data EngineerAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.