From Wikibooks, open books for an open world
Jump to navigation Jump to search

← History | Versions →

This article contains some frequently-asked questions (FAQs) regarding Fortran and their answers.

Q. Should I learn Fortran? Is it obsolete?[edit]

A: Fortran is a good language for scientific programming, because it is fast, is good at handling arrays, is standardized, is portable, and because many high-quality libraries of numerical algorithms exist. The language itself is also maintained; the latest version of the Fortran language is Fortran 2008 (approved by ISO/IEC in September 2010).

Q: What Fortran compiler should I use?[edit]

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 from Polyhedron Software is available at .

Q: How do I create numbered file names such as out_01.txt, out_02.txt etc.?[edit]

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]

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.

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]

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]

A. There is no standard way to do this in Fortran, 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.