[erlang-questions] "Port reading stdout from external program"

Alex Feng sweden.feng@REDACTED
Wed Nov 16 20:06:03 CET 2016


I am using an port to communicate with an external program, I would like to
fetch each output from the external problem.
But I have problem to fetch the output when the external problem runs into
waiting input," scanf " for example.

Here is an example to communicate with a simple C program.

I need to fetch the prompt line("Please input you name") from Port in order
to interact with C code,  but with "scanf" added in C code, I couldn't
fetch it from Port.
Does anyone know why is that ?  Any advice and suggestions will be greatly

Erlang code:

read() ->
  Port = open_port({spawn, "/home/erlang/test/a.out"}, [binary, {line,
255}]), %binary,{line, 255}
  io:format("Port: ~p~n",[Port]),

do_read(Port) ->
    {Port,{data,Data}} ->
        io:format("Data: ~p~n",[Data]);
    Any ->
      io:format("No match fifo_client:do_read/1, ~p~n",[Any])

C code:

int main()

  char str[10];

  printf("Please input you name:");  //Prompt line

  scanf ("%9s",str);  // with this line, port can not read the output from
this program.

  return 1;

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20161116/ac1253ec/attachment.htm>

More information about the erlang-questions mailing list