How to add shell command to Fortran script
Is it possible to call a shell command from a Fortran script file?
My problem is that I am parsing really large files. These files have many lines, eg. 84084002 or similar. I need to know how many lines a file has before I start parsing, so I usually used the shell command: wc -l "filename"
and then used that number as a single variable parameter in my script.
But I would like to call this command from my program and use the number of lines and store it in the value of a variable.
I know it might not work, but if it does, please let me know.
source to share
Since 1984, actually in the 2008 standard, but already implemented by most of the commonly encountered Fortran compilers, including gfortran
, there is a standard internal subroutine execute_command_line
that roughly corresponds to what is widely implemented, the standard subroutine system
. Since @MarkSetchell has (almost) written, you can try
CALL execute_command_line('wc -l < file.txt > wc.txt' )
OPEN(unit=nn,file='wc.txt')
READ(nn,*) count
What Fortran doesn't have is a standard way to get the number of lines in a file without going through an operating system-specific workaround. Other, that is, than opening a file, counting the number of lines, and then rewinding to the beginning of the file to start reading.
source to share