2013-04-18 23 views
0

我正在使用SWI Prolog學習Prolog,並發現與文件上的讀寫操作有關的一些困難。無法訪問Prolog中的文件,總是讀取end_of_file原子

我有這個簡單的程序,在一個文件中讀取從標準輸入的東西(鍵盤),並把它寫:

processFile(File) :- see(File), 
       processFile, 
       seen. 

processFile :- read(Query), 
       process(Query). 

process(end_of_file) :- !. 

process(Query) :- Query, 
        write(Query), 
       nl, 
       processFile. 

我試圖在Linux下執行。所以在bash中我進入所在Prolog的源文件和MYFILE文件和文件夾,在那之後我諮詢了程序,我執行以下statment:

?- processFile(myFile). 
true. 

,你可以看到探針是給我真實的,但我不能通過鍵盤插入任何東西,所以任何東西都可以寫入myFile文件。

如果我嘗試跟蹤發生什麼事我得到以下信息:

[trace] ?- processFile(myFile). 
    Call: (6) processFile(myFile) ? creep 
    Call: (7) see(myFile) ? creep 
    Exit: (7) see(myFile) ? creep 
    Call: (7) processFile ? creep 
    Call: (8) read(_G697) ? creep 
    Exit: (8) read(end_of_file) ? creep 
    Call: (8) process(end_of_file) ? creep 
    Exit: (8) process(end_of_file) ? creep 
    Exit: (7) processFile ? creep 
    Call: (7) seen ? creep 
    Exit: (7) seen ? creep 
    Exit: (6) processFile(myFile) ? creep 
true. 

正如你可以看到它可以訪問到MYFILE文件,並正確地問processFile謂詞包含陳述,但似乎自動讀取end_of_file原子,不能繼續下去。

爲什麼?我能做些什麼來解決它並寫入這個文件?

+0

請,後(部分)文件... – CapelliC

+0

什麼文件? MYFILE?它是空的(我有一個空文件,我將寫入從鍵盤讀取的內容)我缺少什麼?太多了 – AndreaNobili

回答

0

您必須使用tell/1到在文件中寫入,然後告訴恢復到上一個​​。使用見/ 1你閱讀從,並且它是空的......

但ISO IO比較好...