# Running a Fortran program and getting the results

I'm trying to figure how to make Mathematica interact with a Fortran program that takes 2 parameters in a first input line, 3 parameters in a second input line and produces two lines as outputs. Below a simple example to illustrate my point:

program par
implicit none
integer            :: i,j
integer            :: k,l,m

write (*,*) (i+j)*k, (i+j)-2*l
write (*,*) 2*l, 3*m, i-j

end program par


When I run the code below in Mathematica, I get "EndOfFile" as a result.

pr = StartProcess["par"];
g[i_, j_, k_, l_, m_] := (Write[pr, ToString[i], ToString[j]];
g[1, 2, 3, 4, 5]


Any help would be highly appreciated. Cheers.

• Can you clarify a bit? What should Mathematica take as an input, and return exactly? – anderstood Nov 15 '16 at 0:25
• related: mathematica.stackexchange.com/a/77063/2079.. I suspect your Write may be running the numbers together with no spaces but cant test here..try adding ," ", to the lists – george2079 Nov 15 '16 at 1:00
• In the Fortran code given above as an example, the "read" statements are the inputs (variables i and j in the first, and k, l and m in the second), and the "write" are the outputs. – LMelo Nov 15 '16 at 1:42

This worked for me

g[i_, j_, k_, l_, m_] := (
WriteLine[pr, ToString[i] <> " " <> ToString[j]];
WriteLine[pr,
ToString[k] <> " " <> ToString[l] <>
" " <> ToString[m]];

pr = StartProcess["/home/louis/prgms/tmp/par"];
g[1, 2, 3, 4, 5] // ColumnForm


(* " 9 -5", " 8 15 -1" *)

For numeric output, one could use this

g[i_, j_, k_, l_, m_] := (
WriteLine[pr, ToString[i] <> " " <> ToString[j]];
WriteLine[pr,
ToString[k] <> " " <> ToString[l] <>
" " <> ToString[m]];

(* {{9, -5}, {8, 15, -1}} *)
• ReadList[pr,Number,2] may do – george2079 Nov 15 '16 at 2:37
• This also works for numeric output: {Read[pr, {Number, Number}], Read[pr, {Number, Number, Number}]} – LouisB Nov 15 '16 at 2:42