0

I have a cpp application that I have compiled on fly machine

#include <string>

int main() {
    std::string input;
    std::cout << "Please enter some text: ";
    std::getline(std::cin, input);
    std::cout << "You entered: " << input << std::endl;
    return 0;
}

I want to run the compiled executable like this ./program < input.txt .

contents of input.txt looks like this Hello, this is input from a file.

When I send that command over their REST API

{
    "cmd": "./program < input.txt"
}

, I get an error

{
    "error": "deadline_exceeded: Post \"http://unix/v1/exec\": context deadline exceeded"
}

I think it is because of the input redirection command or the cin ?? Any idea on what to do now?

Botje
  • 26,269
  • 3
  • 31
  • 41
Raaz
  • 1,669
  • 2
  • 24
  • 48
  • 1
    What is a fly machine? – Eldinur the Kolibri Aug 25 '23 at 09:58
  • @EldinurtheKolibri sorry. fly machine is a VM in the cloud. https://fly.io/docs/machines/working-with-machines/ – Raaz Aug 25 '23 at 11:11
  • 1
    Random guess: `cmd` is not being interpreted by a shell. Try wrapping your command in a bash invocation like so: `{"cmd": "bash -c \"./program < input.txt\""}` – Botje Aug 25 '23 at 11:59
  • thanks @Botje . `sh -c "./program < input.txt"` passing it like this fixed it. Can you please make your comment the answer so that I can mark it as a soln? – Raaz Aug 25 '23 at 15:16

1 Answers1

0

Apparently this cmd argument is not interpreted by a shell. This means that your program receives the literal arguments < and input.txt but nothing is ever sent to STDIN.

If you wrap your command in a bash -c invocation the shell will do that for you:

bash -c './program < input.txt'
Botje
  • 26,269
  • 3
  • 31
  • 41