您做错了什么与write(f,tecka); 行无关,而是与pascal 中的块规则有关。
如果你想让编译器停止抱怨,你需要将 case 语句更改为:
case znak of
'a'..'z','A'..'Z': begin
seek(f,filepos(f)-1);
write(f,tecka);
end;
end; {case}
问题在于 case 语句在 of 之后需要一个或多个常量表达式(例如 'a'..'z')。在: 之后,您只能放置一个语句或一个开始结束块。因为您在 : 之后放置了 2 个语句,所以 Pascal 想要将第二个语句解释为另一个 case 子句(例如:'0'..'9':),但事实并非如此。
如果您想在一个部分中放置多个语句,您必须始终使用 begin-end 块。
这也是 if 语句中的一个陷阱:
if x then
statement1;
statement2; <-- statement2 will always be executed, because it's not part of the if
这应该是:
if x then begin
statement1;
statement2; <-- statement2 depends on x.
end;
正是由于这个原因,许多人总是使用开始-结束块。
即使只有一条语句。
如果您稍后更改代码,您只需将额外的语句放在已经存在的 begin-end 块中,并且不要忘记将其放入。
请注意,assignfile-reset-seek 等调用已经过时了。
在 Delphi 中,使用字符串列表进行处理会更有效。
procedure TForm1.Button3Click(Sender: TObject);
var
MyText: TStringList;
i,a: integer;
znak,tecka:char;
const
Alpha = 'a'..'z','A'..'Z';
Filename = 'Znaky.dat';
begin
tecka:='.';
MyText:= TStringlits.create;
MyText.LoadFromFile(Filename);
//loop though all lines.
for i:= 0 to MyText.Lines.Count-1 do begin
//loop though every char in a line (string chars start counting from 1 !)
for a:= 1 to Length(MyText.Lines[i]) do begin
case MyText.Lines[i][a] of
Alpha: begin
MyText.Lines[i][a]:= tecka;
end;
end; {case}
end; {for}
end; {for}
MyText.WriteToFile(Filename);
end;
这更高效,因为您只需执行两次磁盘 IO,而不是多次。
个人挑剔:请不要将case 和while 之类的关键字大写,这是Pascal,而不是VB。