2015-06-20 46 views
0

可執行文件說有,我已經使用編譯成可執行文件的C++代碼:運行使用哈斯克爾

g++ test.cpp -o testcpp 

我可以運行此使用終端(我使用的是OS X),並提供輸入文件用於C++程序內側處理,如:

./testcpp < input.txt 

我想知道如果這樣做是可能的,從 Haskell中。我聽說System.Process模塊中的readProcess函數。但是這隻允許運行系統shell命令。

這樣做:

out <- readProcess "testcpp" [] "test.in" 

或:

out <- readProcess "testcpp < test.in" [] "" 

或:

out <- readProcess "./testcpp < test.in" [] "" 

拋出這個錯誤(或非常類似的東西,這取決於上面我用的一個):

testcpp: readProcess: runInteractiveProcess: exec: does not exist (No such file or directory) 

所以我的問題是,是否可以從Haskell做到這一點。如果是這樣,我應該如何以及使用哪些模塊/功能?謝謝。

編輯

好了,大衛建議,我刪除了輸入參數,並試圖運行它。這樣做的工作:

out <- readProcess "./testcpp" [] "" 

但我仍然堅持提供輸入。

+0

在第一部分中,你有一個可執行文件名爲'test',並在第二部分你想運行一個名爲'testcpp'的可執行文件。它是否正確?另外我認爲你需要讀取'test.in'的內容,然後傳遞該字符串作爲最後一個參數,而不是給出文件名。 –

+0

@DavidYoung是的,我只是提供了我想要做的一般事例。但是,由於它似乎很混亂,我編輯了它。至於你的評論的第二部分,並不完全適合你。 AFAIK,問題是運行exec文件本身,而不是提供輸入參數。 – Roshnal

+0

您始終可以使用shell腳本創建文件,然後運行* it *。 –

回答

4

documentation for readProcess說:

readProcess 
    :: FilePath Filename of the executable (see RawCommand for details) 
    -> [String] any arguments 
    -> String  standard input 
    -> IO String stdout 

當它要求standard input它不要求輸入文件來讀取輸入,但是對於文件標準輸入的實際內容。

所以你需要使用readFile等來獲得test.in內容:

input <- readFile "test.in" 
out <- readProcess "./testcpp" [] input 
+0

是的!這工作。感謝您的回答和明確的解釋:) – Roshnal