使用 Haskell 运行可执行文件

Ros*_*nal 0 shell executable haskell command-line-arguments

假设有一个 C++ 代码,我使用以下命令编译成可执行文件:

g++ test.cpp -o testcpp
Run Code Online (Sandbox Code Playgroud)

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

./testcpp < input.txt
Run Code Online (Sandbox Code Playgroud)

我想知道从Haskell内部这样做是否可行。我听说过模块中的readProcess函数System.Process。但这仅允许运行系统 shell 命令。

这样做:

out <- readProcess "testcpp" [] "test.in"
Run Code Online (Sandbox Code Playgroud)

或者:

out <- readProcess "testcpp < test.in" [] ""
Run Code Online (Sandbox Code Playgroud)

或者:

out <- readProcess "./testcpp < test.in" [] ""
Run Code Online (Sandbox Code Playgroud)

抛出此错误(或非常相似的错误,具体取决于我使用的是上述哪一个):

testcpp: readProcess: runInteractiveProcess: exec: does not exist (No such file or directory)
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,Haskell 是否可以这样做。如果是这样,我应该如何使用以及使用哪些模块/功能?谢谢。

编辑

好的,正如大卫建议的那样,我删除了输入参数并尝试运行它。这样做有效:

out <- readProcess "./testcpp" [] ""
Run Code Online (Sandbox Code Playgroud)

但我仍然坚持提供输入。

ram*_*ion 5

文档readProcess说:

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

当它要求时,standard input它不是要求一个文件来读取输入,而是要求该文件的标准输入的实际内容。

因此,您需要使用readFile或类似的方法来获取以下内容test.in

input <- readFile "test.in"
out <- readProcess "./testcpp" [] input
Run Code Online (Sandbox Code Playgroud)