0

Say there's a C++ code that I've compiled into an executable using:

g++ test.cpp -o testcpp

I can run this using Terminal (I'm using OS X), and provide an input file for processing inside the C++ program, like:

./testcpp < input.txt

I was wondering if doing this is possible, from within Haskell. I have heard about the readProcess function in System.Process module. But that only allows for running system shell commands.

Doing this:

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

or:

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

or:

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

throws out this error (or something very similar depending on which one of the above I use):

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

So my question is, whether doing this is possible from Haskell. If so, how and which modules/functions should I use? Thanks.

EDIT

Ok, so as David suggested, I removed the input arguments and tried running it. Doing this worked:

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

But I'm still stuck with providing the input.

3
  • In the first part, you have an executable named test and in the second part you're trying to run an executable named testcpp. Is this correct? Also I think you need to read the contents of test.in and then pass that string as the last argument, rather than giving the file name. Commented Jun 20, 2015 at 18:58
  • @DavidYoung Yes, I just provided general examples of what I was trying to do. But as it appears to be confusing, I edited it. As for the second part of your comment, didn't quite get you. AFAIK, the problem is with running the exec file itself, and not with providing the input arguments. Commented Jun 20, 2015 at 19:11
  • you can always create a file with a shell script, and run it. Commented Jun 20, 2015 at 20:10

1 Answer 1

6

The documentation for readProcess says:

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

When it's asking for standard input it's not asking for a file to read the input from, but the actual contents of standard input for the file.

So you'll need to use readFile or the like to get the contents of test.in:

input <- readFile "test.in"
out <- readProcess "./testcpp" [] input
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.