一个简单的问题!聪明的程序员用delphi,聪明的程序员来回答这个问题

2002past 2003-09-08 08:00:27
对一个文本文件进行写操作,如何进行? 现在我知道的就是用 writefile 和filewrite 这两个函数,但是我对这两个的用法搞不对,如果你知道如何用这两个 或者你知道其它的方法,请尽情的表达!
waiting for you!
...全文
28 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
littleisland 2003-09-11
  • 打赏
  • 举报
回复
TFileStream
2002past 2003-09-11
  • 打赏
  • 举报
回复

如果大家有兴趣,请再参与如下帖:
http://expert.csdn.net/Expert/topic/2163/2163691.xml?temp=.1014673
http://expert.csdn.net/Expert/topic/2185/2185708.xml?temp=.255398
2002past 2003-09-11
  • 打赏
  • 举报
回复
好了
结贴了
由衷的感谢以下人员:
hkbarton(宁静至远||淡泊明志)
madyak(无天)
wypepsi(百事可乐)
lemon_wei(soft_fans)
等delphi程序员的支持和帮助。
lemon_wei 2003-09-09
  • 打赏
  • 举报
回复
var

F: TextFile;
S: string;
begin
S:='123245678';
if OpenDialog1.Execute then { Display Open dialog box }
begin
AssignFile(F, OpenDialog1.FileName); { File selected in dialog }
Rewrite(F);

writeln(F, S);
CloseFile(F);
end;
end;
2002past 2003-09-09
  • 打赏
  • 举报
回复
OK
挺不错的,不知道这里有没有人对tidtelnet这个控件比较熟悉的人
谁知道这个控件里sendcmd这个方法如何使用,关健是它的返回数据我如何得到。
wypepsi 2003-09-09
  • 打赏
  • 举报
回复
教你一个土办法 动态创建一个memo 然后
memo.lines.loadfromfile('yourfile');

然后
情况1 memo.lines.add('祝所有程序员朋友中秋节快乐!');
情况2 memo.lines.strings[i]:='祝所有程序员朋友中秋节快乐!'i是你自己定义的行

最后memo.lines.Savetofile('yourfile');
呵呵 比较落后
hkbarton 2003-09-09
  • 打赏
  • 举报
回复
常规的文本文件操作方法:
var
MyFile:TextFile;
begin
AssignFile(MyFile,'d:\myfile.txt');
ReWrite(MyFile);
//创建或覆写一个文件,当你你后要追加或而不是覆盖文件的时候用Append(MyFile)
try
WriteLn(MyFile,greet);
finally
CloseFile(MyFile);
end;
end;
2002past 2003-09-09
  • 打赏
  • 举报
回复
朋友,如果你有异议,直截举例说明。
我很希望大家能把一个问题吃透搞彻底,搞明白。
学无止境,大家可以共同进步嘛!
y740504 2003-09-09
  • 打赏
  • 举报
回复
呵呵我觉得用MEMO不行吗?
每次调入文本文件我保存到文本文件不就行了?
即简单又方便
lchy20cn 2003-09-09
  • 打赏
  • 举报
回复
mk
2002past 2003-09-09
  • 打赏
  • 举报
回复
话说曹操一直看见蒋干就不爽.这天,他想奸蒋干一次,就在看见蒋干时对他说:"干,你娘,好吗?" 蒋干一听,暴不爽,于是问操:"操,你全家,好吗?"
2002past 2003-09-08
  • 打赏
  • 举报
回复
to: hkbarton(宁静至远||淡泊明志)

用你说的tstringlist不行,后面再调用它的话,它会覆盖掉前面保存的东东
2002past 2003-09-08
  • 打赏
  • 举报
回复
这个我也看到了
但是理解上好像有点不对,用的时候老是语法错误,说参数类型不匹配之类的话
可不可以下例来做做
var
greet: string;

....

greet:='祝所有程序员朋友中秋节快乐!'

把这句加入一个文件中!用filewrite和writefile
hkbarton 2003-09-08
  • 打赏
  • 举报
回复
Delphi syntax:

function FileWrite(Handle: Integer; const Buffer; Count: Integer): Integer;

Description

FileWrite writes Count bytes to the file given by Handle from the buffer specified by Buffer. Handle is a file handle returned by the FileOpen or FileCreate method.

The return value is the number of bytes actually written, or -1 if an error occurred.
madyak 2003-09-08
  • 打赏
  • 举报
回复
The following example uses a button, a string grid, and an Open dialog box on a form. When the button is clicked, the user is prompted for a filename. When the user clicks OK, the specified file is opened, read into a buffer, and closed. Then the buffer is displayed in two columns of the string grid. The first column contains the character values in the buffer. The second column contains the numeric values of the characters in the buffer.

procedure TForm1.Button1Click(Sender: TObject);

var
iFileHandle: Integer;
iFileLength: Integer;
iBytesRead: Integer;
Buffer: PChar;
i: Integer
begin
if OpenDialog1.Execute then
begin
try
iFileHandle := FileOpen(OpenDialog1.FileName, fmOpenRead);
iFileLength := FileSeek(iFileHandle,0,2);
FileSeek(iFileHandle,0,0);
Buffer := PChar(AllocMem(iFileLength + 1));
iBytesRead := FileRead(iFileHandle, Buffer^, iFileLength);

FileClose(iFileHandle);
for i := 0 to iBytesRead-1 do
begin
StringGrid1.RowCount := StringGrid1.RowCount + 1;
StringGrid1.Cells[1,i+1] := Buffer[i];
StringGrid1.Cells[2,i+1] := IntToStr(Integer(Buffer[i]));
end;
finally
FreeMem(Buffer);
end;
end;
end;
madyak 2003-09-08
  • 打赏
  • 举报
回复
he 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;
2002past 2003-09-08
  • 打赏
  • 举报
回复
好,问题解决,大哥,小弟有礼了,中秋节快乐

不过小弟还想问一问是否知道filewrite 或 writefile 这两个函数的用法

祝所以有程序员朋友中秋节快乐呀!
hkbarton 2003-09-08
  • 打赏
  • 举报
回复
你建立以后一定没有关闭它!FileClose
2002past 2003-09-08
  • 打赏
  • 举报
回复
谢谢,不过显示有错,说是另一程序正在使用这个文件
不知道是不是其它地方用了这个,说明一下
我这个文件是刚建立的
我是用 filecreate
来创建成的
hkbarton 2003-09-08
  • 打赏
  • 举报
回复
stringlist:=Stringlist.Create;
try
stringlist.Add(greet);
stringlist.SaveToFile('路径');
finally
stringlist.free;
end;
加载更多回复(10)

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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