This article contains some frequently-asked questions (FAQs) regarding Fortran and their answers.
Q. Should I learn Fortran? Is it obsolete?[edit | edit source]
A: Fortran is not obsolete and it will not be any time soon. Fortran is a general purpose programming language and is suitable for many applications. However, it excells at numerical computation and high performance computing. It is fast, portable and it has seamless handling of arrays. Because of this, there are many high-quality Fortran libraries for numerical algorithms and it is widespread in scientific communities (e.g. numerical weather predicion). The language itself is still maintained and regularly updated with modern features; the latest version is Fortran 2018.
Q: What Fortran compiler should I use?[edit | edit source]
A: Oracle Solaris Studio, GNU Fortran, G95, and Silverfrost (Windows only) are free software Fortran 95 compilers, and Absoft, IBM, Intel, Lahey, NAG, Pathscale and PGI sell Fortran compilers. Comparative information on Fortran compilers is available at Wikipedia.
Q: How do I create numbered file names such as out_01.txt, out_02.txt etc.?[edit | edit source]
A: Use an "internal write" to create the file names, for example
write (file_name,"('out_',i2.2,'.txt')") i
A: A neater way to do this would be:
i=<file number> WRITE(file_name, fmt = '(A4,I0,A4)')'out_',i,'.txt'
This way the formatting is clear and you are writing the correct string to the variable. I assume you wanted an integer as the number. 'I' format statement requires an Integer length, or zero in some cases. You are probably thinking of F where the decimal point denotes the number of decimals to consider.
Q: What does the open statement look like using this technique? OPEN(UNIT = __, FILE = ???, STATUS='NEW')?[edit | edit source]
A: Gfortran does not accept this block
write(file_name,'cp',(i5.5),'.out') ITN open (67,file = file_name)
Gives, ERROR, file tag must be of type CHARACTER
Can someone else help with this?
A: See the answer above. Basically the way you have written the variable file_name is incorrect.
WRITE(file_name,fmt='(A2,I0,A4)')'cp',ITN,'.out' OPEN(UNIT=67, file=file_name, status='new')
Assuming that ITN has been declared as an integer and given a value.
Q: How can I convert a string to an integer and other types? What about the reverse?[edit | edit source]
A: Use an "internal read" or "internal write". In effect, you use a character variable as the file name and read or write the I/O list. You write things like
read (character_variable, format) list of variables
to convert a string to another type and
write (character_variable, format) list of variables
to convert to a string from another type, as demonstrated by the following program:
program xconvert_integer_string character(20) :: cnum integer :: i i = 445 write(cnum,'(i5)') i write(*,'(a)') trim(cnum) ! should output " 445" write(cnum,'(i5.5)') i write(*,'(a)') trim(cnum) ! should output "00445" i = 34 write(cnum,'(i0)') i write(*,'(a)') trim(cnum) ! should output "34" end program xconvert_integer_string
This answer is based on messages in comp.lang.fortran by Paul van Delst and Dick Hendrickson.
Q. How do I issue a command to the operating system within a Fortran program?[edit | edit source]
A. The Fortran 2008 standard provides the
execute_command_line intrinsic procedure to make system calls. Prior versions have no standard way to make system calls, but many compilers have an extension named "system" or something similar. Please consult the documentation of your compiler. In Fortran 2003, one can call C code, and it is possible to call the operating system from C.