Jam*_*tam 1 delphi pascal var compiler-errors lazarus
以下是我正在处理的文件预览系统项目的示例.主窗体上有两个ListBox.第一个[lst_fileList]显示目录[files]中所有".txt"文件的列表,每个文件都标有[order ###.txt],###是1到999之间的任意数字.程序运行后,它会在列表框中找到所选项目(.txt文件),然后在第二个ListBox [lst_filePreview]上显示文件中的每一行.
虽然在运行它时,ReadLn(selectedFile)的第21行发生错误.错误状态(不兼容类型:得到"无类型",预期"AnsiString").
我已经看了几个小时这个错误,无济于事...任何帮助将不胜感激,谢谢.
procedure TForm1.btn_getPreviewClick(Sender: TObject);
var
checkSelect:integer;
orderSelect:string;
i:integer;
selectedFile:textFile;
begin
if lst_fileList.SelCount > 0 then
begin
for checkSelect:= 0 to (lst_fileList.Items.Count - 1) do
if lst_fileList.Selected [checkSelect] then
begin
orderSelect:=lst_fileList.Items[checkSelect];
orderSelect:=RightStr(orderSelect,3);
if fileexists('files\order'+orderSelect+'.txt') then
begin
assignFile(selectedFile,'files\order'+orderSelect+'.txt');
reset(selectedFile);
while not EOF(selectedFile) do
begin
lst_filePreview.Items.Add(readLn(selectedFile)); // Error occurs here: //
end;
closeFile(selectedFile);
end;
end;
end else
ShowMessage('Please select an item first!');
end;
Run Code Online (Sandbox Code Playgroud)
你的代码
lst_filePreview.Items.Add(readLn(selectedFile));
Run Code Online (Sandbox Code Playgroud)
尝试使用Readln作为函数.它不是.它正式是一个过程,就像一个返回void的函数(无类型).实际上,它是一个编译器 - 魔术过程,并且根据它实际尝试读取的内容,编译器会将调用插入到不同的运行时函数或过程中.
您可能希望完全摆脱旧的Pascal样式例程,而是使用流,但是现在,请尝试:
s: string
...
Readln(selectedFile, s);
lst_filePreview.Items.Add(s);
Run Code Online (Sandbox Code Playgroud)
请阅读Standard Routines和Input-Output上的Delphi DocWiki说明,说:
注意:对于新程序,您可能希望使用System.Classes和System.SysUtils单元中的文件管理类和函数.System.Classes.TStream及其后代类目前推荐用于Delphi中的常规文件处理(相关例程,请参阅Streams,Reader和Writers).对于文本文件处理,建议使用TStreamReader和TStreamWriter而不是调用Write和Writeln.API类别索引包含相关例程和类的列表.
如果你lst_filePreview
的确是一个TListBox
,你甚至可以这样做:
lst_filePreview.Items.LoadFromFile('files\order'+orderSelect+'.txt');
Run Code Online (Sandbox Code Playgroud)
并保存自己的整个阅读代码.我可能会用一个TMemo
而不是做:
FilePreviewMemo.Lines.LoadFromFile('files\order'+orderSelect+'.txt');
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1080 次 |
最近记录: |