2015-06-20 63 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