2014-10-12 43 views
1

下面是我正在处理的文件预览系统项目的一个示例。主窗体上有两个ListBox。第一个[lst_fileList]显示目录[files]中所有“.txt”文件的列表,每个文件都标有[order ###。txt],其中###是1到999之间的任意数字。运行该过程,它会在列表框中找到所选项(.txt文件),然后在第二个ListBox [lst_filePreview]中显示文件内的每一行。德尔福错误:得到“无类型”,预计“AnsiString”

尽管在我运行它时,在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; 
+0

在我看到的问题,它被声明为 “文本文件”。 – 2014-10-12 14:14:20

回答

6

您的代码

lst_filePreview.Items.Add(readLn(selectedFile)); 

尝试使用Readln的功能。不是这样。它正式是一个过程,就像一个函数返回void(无类型)。实际上,它是一个编译魔术过程,根据它实际尝试读取的内容,编译器将调用插入到不同的运行时函数或过程中。

你可能想摆脱旧的Pascal风格的套路完全和使用流来代替,但现在,试试:

s: string 

    ... 

    Readln(selectedFile, s); 
    lst_filePreview.Items.Add(s); 

请阅读德尔福DocWiki注意到在Standard Routines and Input-Output,他说:

Note: For new programs, you might want to use the File Management classes and functions in the System.Classes and System.SysUtils units. System.Classes.TStream and its descendent classes are currently recommended for general file handling in Delphi (for related routines, see Streams, Reader and Writers). For text-file handling, TStreamReader and TStreamWriter are recommended over calling Write and Writeln. API Categories Index contains lists of related routines and classes.

如果你lst_filePreview事实上是一个TListBox,你甚至可以这样做:

lst_filePreview.Items.LoadFromFile('files\order'+orderSelect+'.txt'); 

并保存你自己的整个阅读代码。我可能会使用一个TMemo,而不是做:

FilePreviewMemo.Lines.LoadFromFile('files\order'+orderSelect+'.txt'); 
+0

是的,这是行不通的。非常感谢你。 我知道我错过了一些小细节... – 2014-10-12 14:20:22

+1

看看我的编辑:使用LoadFromFile并节省自己的麻烦。 – 2014-10-12 14:27:33

+0

啊,哇! 再次感谢你,这太容易了。 – 2014-10-12 14:36:35

2

使用Readln,你需要使用一个变量。

试试这个:

var 
checkSelect:integer; 
orderSelect:string; 
i:integer; 
selectedFile:textFile; 
SelectedLine : String; 
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 
      readLn(selectedFile, SelectedLine) 
      lst_filePreview.Items.Add(SelectedLine);// Error occurs here:   
      end; 
     closeFile(selectedFile); 
     end; 
     end; 
     end else 
     ShowMessage('Please select an item first!'); 
     end; 
+0

谢谢,这似乎工作。 – 2014-10-12 14:41:33