1

I have a Fortran executable that takes a couple keyboard inputs before doing a calculation. It works in the terminal entering the inputs, but I want to using piping to enter the inputs (Windows 10 command line). When I only have one keyboard input, the piping works perfectly. How can I get it to work with multiple inputs?

I can almost get it to work based on a similar question here: Passing arguments to interactive fortran program. I reviewed documentation on standard inputs, but I still can't figure it out.

Here is an example Fortran program with only one input, and it works with piping.

      program foo
      integer n
      character*80 c
      
      write (*,*) 'Enter 1,2,3 for cat,dog,fish'
      read (*,*) n
      if (n .eq. 1) then
      write (*,*) 'meow'
      elseif (n .eq. 2) then
      write (*,*) 'woof'
      elseif (n .eq. 3) then
      write (*,*) 'blurp'
      else
      write (*,*) 'error1'
      endif
      
C     write (*,*) 'Enter y,n for yay,nay'
C     read (*,*) c
C     if (c == 'y') then
C     write (*,*) 'yes'
C     elseif (c == 'n') then
C     write (*,*) 'no'
C     elseif (n .eq. 3) then
C     else
C     write (*,*) 'error2'
C     endif

      end

Terminal test:

C:\my\file\path> C:\my\file\path\foo.exe
 Enter 1,2,3 for cat,dog,fish
2
 woof

C:\my\file\path> echo 1 | C:\my\file\path\foo.exe
 Enter 1,2,3 for cat,dog,fish
 meow

Here is an example Fortran program with multiple inputs, and it doesn't work with piping.

Same program as above, but commented lines are uncommented. Terminal test:

C:\my\file\path> C:\my\file\path\foo.exe
 Enter 1,2,3 for cat,dog,fish
3
 blurp
 Enter y,n for yay,nay
n
 no

C:\my\file\path> echo 1 y | C:\my\file\path\foo.exe
 Enter 1,2,3 for cat,dog,fish
 meow
 Enter y,n for yay,nay
At line 18 of file C:/my/file/path/foo.f (unit = 5, file = 'stdin')
Fortran runtime error: End of file

Error termination. Backtrace:

Could not print backtrace: libbacktrace could not find executable to open
#0  0x318dd91b
#1  0x318d6b34
#2  0x318d355b
#3  0x318d7f6c
#4  0x318e8e9d
#5  0x318d88df
#6  0x318d5190
#7  0x318b1691
#8  0x318f3f93
#9  0x318b13c0
#10  0x318b14f5
#11  0xb9677c23
#12  0xba24d4d0
#13  0xffffffff

Compiler information: GNU gfortran, CMake based off wiki example

a11
  • 3,122
  • 4
  • 27
  • 66
  • 2
    The answer to this depends on how the program works. If it does not have command line parameters or does not take stdin, then it will not work as a "one-liner". There might be some "record and playback" program the would work. This question is not about source code, so it is off-topic for SO. Consult your foo.exe user documentation. – lit Sep 21 '21 at 19:13
  • Please use the [tag:fortran] tag for all Fortran questions. Fortran 90 is just one very old revision of the standard. – Vladimir F Героям слава Sep 24 '21 at 16:26

2 Answers2

2

You have two simple read statements. Each reads one record = one line of text. You need to provide two records or let the program continue at the old record.

In I can do echo -e "2 \n y" | ./a.out to get two records into one echo command, but I am not sure about Windows cmd. In Powershell you should use Echo newline to powershell console. That means

echo "2 `n y"

If you can change your Fortran code, use non-advancing input:

read (*,'(i1)',advance="no") n

With this change I can do

> echo -e "2 y" | ./a.out 
 Enter 1,2,3 for cat,dog,fish
 woof

Tested in bash, but should work elsewhere including cmd.

2

For windows cmd, just you can use a for loop.

(for %i in (2 y) do @echo %i)|C:\my\file\path\foo.exe
Enter 1,2,3 for cat,dog,fish
woof
Enter y,n for yay,nay
yes

If you are putting this in a .bat or .cmd, remember to use two % signs. There are lots of ways of doing this. See How can I echo a newline in a batch file?

cup
  • 7,589
  • 4
  • 19
  • 42