?
Solved

A problem with appending a text file ?

Posted on 2003-02-26
5
Medium Priority
?
211 Views
Last Modified: 2010-04-04
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
0
Comment
Question by:Mark Brady
[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 21

Expert Comment

by:ziolko
ID: 8024594
FileCreate returns handle to created file You have to release this handle with FileClose();

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

ziolko.
0
 
LVL 20

Author Comment

by:Mark Brady
ID: 8027428
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
 
LVL 34

Expert Comment

by:Slick812
ID: 8028406
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
 
LVL 3

Accepted Solution

by:
ILE earned 300 total points
ID: 8029447
instead of use file create use

var g:textfile;


supstitute filecreate('name')  with


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

Author Comment

by:Mark Brady
ID: 8031232
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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

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