Mark Brady
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
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
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
// 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
Flush(g);
Closefile(g);
end;
Finally
Templist.free;
end;
end;
This only works if the file exists before run the application.
Mark
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
procedure TForm1.Button2Click(Sender
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.Fil eName) then
begin
BackupName := ExtractFileName(SaveDialog 1.FileName );
BackupName := ChangeFileExt(BackupName, '.BAK');
if not RenameFile(SaveDialog1.Fil eName, BackupName) then
raise Exception.Create('Unable to create backup file.');
end;
FileHandle := FileCreate(SaveDialog1.Fil eName);
{ Write out the number of rows and columns in the grid. }
FileWrite(FileHandle,
StringGrid1.ColCount, SizeOf(StringGrid1.ColCoun t));
FileWrite(FileHandle,
StringGrid1.RowCount, SizeOf(StringGrid1.RowCoun t));
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;
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
var
BackupName: string;
FileHandle: Integer;
StringLen: Integer;
X: Integer;
Y: Integer;
begin
if SaveDialog1.Execute then
begin
if FileExists(SaveDialog1.Fil
begin
BackupName := ExtractFileName(SaveDialog
BackupName := ChangeFileExt(BackupName, '.BAK');
if not RenameFile(SaveDialog1.Fil
raise Exception.Create('Unable to create backup file.');
end;
FileHandle := FileCreate(SaveDialog1.Fil
{ Write out the number of rows and columns in the grid. }
FileWrite(FileHandle,
StringGrid1.ColCount, SizeOf(StringGrid1.ColCoun
FileWrite(FileHandle,
StringGrid1.RowCount, SizeOf(StringGrid1.RowCoun
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
FileWrite(FileHandle, StringLen, SizeOf(StringLen));
FileWrite(FileHandle,
StringGrid1.Cells[X,Y], Length(StringGrid1.Cells[X
end;
end;
FileClose(FileHandle);
end;
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
var FileHandle: Integer;
FileHandle:=FileCreate(f);
CloseFile(FileHandle);
ziolko.