文本文件读写问题:每次向文本文件中写入一行文本(回车结尾)

lovelypengpeng 2003-08-06 09:18:14
当文件不存在时创建该文件,否则打开该文件向文件末尾添加内容。
...全文
144 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hiflower 2003-08-22
  • 打赏
  • 举报
回复
同上
wsforqyc 2003-08-22
  • 打赏
  • 举报
回复
看你小子太小气了,一分都不给,不过做个好人,下次大方点...
procedure TForm1.Button1Click(Sender: TObject);
var
F : TextFile;
Str : string;
FileName : string;
begin
AssignFile(F,FileName);
if FileExists(FileName) then
Append(F)
else
ReWrite(F);
WriteLn(Str);
CloseFile(F);
end;
hare007 2003-08-22
  • 打赏
  • 举报
回复
mark
dashi888 2003-08-08
  • 打赏
  • 举报
回复
接分愉快,谢谢!
xiufengy 2003-08-07
  • 打赏
  • 举报
回复
unit Log;

interface

uses
SysUtils, Forms, Dialogs;

procedure MyAddStr(MyStr:string);
implementation
procedure MyAddStr(MyStr:string);
Var
MothodFile:TextFile;
FileName:String;
FileOpened:Boolean;
i,j:Integer;
Str1:array[0..100] of string;
str4:string;
Begin
i:=0;
FileName:=ExtractFilePath(Application.Exename)+'log.txt';
AssignFile(MothodFile,FileName);
Try
Reset(MothodFile);
FileOpened:=True;
Except
On EInOutError Do
Begin
Try
if FileExists(FileName)=False then
Begin
ReWrite(MothodFile);
FileOpened:=True;
End
Else
Begin
FileOpened:=False;
ShowMessage('文件不能打开');
End;
Except
On EInOutError Do
Begin
FileOpened:=False;
ShowMessage('文件不能创建');
End;
End;
End;
End;
if fileopened=false then exit;
While not eof(MothodFile) and (i<99) do
Begin
Readln(MothodFile,Str4);
Str1[i]:=trim(str4);
inc(i);
end;
CloseFile(MothodFile);
AssignFile(MothodFile,FileName);
ReWrite(MothodFile);
Writeln(MothodFile,mystr+' At:'+datetimetostr(now));
for j:=0 to i do
Begin
str4:=Str1[j];
Writeln(MothodFile,str4);
End;
CloseFile(MothodFile);

End;

end.
magicmouse 2003-08-06
  • 打赏
  • 举报
回复
TFileStream是没法支持你的这种需要,但办法总是有的,如下代码所示:
procedure TForm1.Button1Click(Sender: TObject);
var fs:TFileStream;
s:string;
fn:string;
begin
fn:= 'd:\temp\test.txt';
if not FileExists(fn) then
FileClose(FileCreate(fn));
fs:= TFileStream.Create(fn,fmOpenReadWrite);
try
fs.Seek(0,soFromEnd);
s:= DateTimeToStr(Now)+ chr(13)+chr(10);
fs.Write(PChar(s)^,Length(s));
finally
fs.Free;
end;
end;
lovelypengpeng 2003-08-06
  • 打赏
  • 举报
回复
我用TFileStream 没有试成功

828

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧