使用Haskell运行可执行文件

时间:2015-06-20 17:52:55

标签: shell haskell command-line-arguments executable

说我已经使用以下代码编译成可执行文件的C ++代码:

g++ test.cpp -o testcpp

我可以使用终端(我使用OS X)运行它,并提供一个输入文件,用于在C ++程序内部进行处理,如:

./testcpp < input.txt

我想知道是否可以从 Haskell中执行此操作。我听说过readProcess模块中的System.Process函数。但这只允许运行系统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" [] ""

但我仍然坚持提供输入。

1 个答案:

答案 0 :(得分: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