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