2014-10-12 46 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