如何实现让用户来选择保存的位置。(20分)

  • 主题发起人 tswhoney
  • 开始时间
T

tswhoney

Unregistered / Unconfirmed
GUEST, unregistred user!
我的“昆明200205”是个已存在的文本文件,现在就是要让用户来选择保存的位置,硬盘或软盘。
我这样写有问题:
if savedialog1.Execute then
assignfile(copytxt,savedialog1.FileName);

copytxt就是已存在的文本文件“昆明200205”,该如何写?
 
if savedialog1.Execute then
begin
assignfile(copytxt,savedialog1.FileName);
ReWrite(CopyText);
......
end;
 
ReWrite(CopyText);
你这样做就覆盖了原文件的内容了,我的要求是不能覆盖原文件。
 
DELPHI已太久没有使用了,我真想不起怎么做了。不过,可以提醒你,把Seek移到文件后面再写的。
 
procedure TForm1.Button1Click(Sender: TObject);
var F: TextFile;
S: String;
begin
if SaveDialog1.Execute then
begin
S := SaveDialog1.FileName;
if (FileExists(S)) then
begin
AssignFile(F, S);
Append(F);
WriteLn(F, DateTimeToStr(Now));
Flush(F);
CloseFile(F);
end
else
MessageBox(Handle, PChar(Format('文件"%s"不存在!', )), '错误', MB_ICONERROR);
end;
end;
(修改了一下)
 
用fileexists(savedialog1.FileName)先判断有没有这文件存在
如果不存在savetofile(savedialog1.FileName)

 
to Sachow:我将代码贴出来了,你帮我看一下,还是不行,提示文件不存在,可我明明创建好了。

procedure TForm3.BitBtn5Click(Sender: TObject);
var
copytxt:textfile;
s,s1,sd,s2:string;
begin
//panel3.Visible:=true;
sd:=copy(label2.caption,7,2);
dm3.ADOquery2.Close;
dm3.adoquery2.sql.Clear;
dm3.ADOQuery2.sql.Add('select * from detailed_table where adata='+quotedstr(sd)+'order by uid');
dm3.ADOquery2.open;
dbsumlist1.DataSet:=dm3.adoquery2;
str1:=currtostr(dbsumlist1.SumCollection.Items[0].SumValue);
str2:=currtostr(dbsumlist1.SumCollection.Items[1].SumValue);
str3:=currtostr(dbsumlist1.SumCollection.Items[2].SumValue);
str4:=currtostr(dbsumlist1.SumCollection.Items[3].SumValue);
str5:=currtostr(dbsumlist1.SumCollection.Items[4].SumValue);
str6:=currtostr(dbsumlist1.SumCollection.Items[5].SumValue);
str7:=currtostr(dbsumlist1.SumCollection.Items[6].SumValue);
str8:=currtostr(dbsumlist1.SumCollection.Items[7].SumValue);
str9:=currtostr(dbsumlist1.SumCollection.Items[8].SumValue);
str10:=currtostr(dbsumlist1.SumCollection.Items[9].SumValue);
str11:=currtostr(dbsumlist1.SumCollection.Items[10].SumValue);
str12:=currtostr(dbsumlist1.SumCollection.Items[11].SumValue);
str13:=currtostr(dbsumlist1.SumCollection.Items[12].SumValue);
str14:=currtostr(dbsumlist1.SumCollection.Items[13].SumValue);
str15:=currtostr(dbsumlist1.SumCollection.Items[14].SumValue);
s1:=copy(dm3.ADOTable3.Fields[1].AsString,1,4)+copy(label2.Caption,1,10);
assignfile(copytxt,'c:/'+s1+'.txt');
s2:='c:/'+s1+'.txt';
rewrite(copytxt);
try
begin
s:='编制单位:'+dm3.ADOTable3.Fields[1].AsString;
writeln(copytxt,s);
writeln(copytxt,str1);
writeln(copytxt,str2);
writeln(copytxt,str3);
writeln(copytxt,str4);
writeln(copytxt,str5);
writeln(copytxt,str6);
writeln(copytxt,str7);
writeln(copytxt,str8);
writeln(copytxt,str9);
writeln(copytxt,str10);
writeln(copytxt,str11);
writeln(copytxt,str12);
writeln(copytxt,str13);
writeln(copytxt,str14);
writeln(copytxt,str15);
end;
savedialog1.FileName:=s1;
savedialog1.Filter:='txt';
if saveDialog1.Execute then
begin
AssignFile(copytxt, saveDialog1.FileName);
Append(copytxt);
Flush(copytxt);
CloseFile(copytxt);
end;
finally
closefile(copytxt);
end;



end;
 

……
assignfile(copytxt,'c:/'+s1+'.txt');
s2:='c:/'+s1+'.txt';
rewrite(copytxt);
try
begin
……
你在这里并没有关闭这个文件,如果这是一个新文件,那此时它还未被写到磁盘上,之后你
又再次打开这个文件,当然会提示文件不存在了。
 
我改成
……
assignfile(copytxt,'c:/'+s1+'.txt');
s2:='c:/'+s1+'.txt';
rewrite(copytxt);
closefile(copytxt);

try
begin
……
还是不行,那改怎末改?
 
在closefile(copytxt)行设断点,按F7退步跟踪,把鼠标指在各个变量上,看它们是什么值。
接下来你应该能根据这些变量的值自己判断情况了。
 
值都是对的,可以保存在c盘上。
 
多人接受答案了。
 
顶部