Fortran_Lecture_6

Fortran_Lecture_6 - Fortran Class Notes Lecture 6 File...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Fortran Class Notes Lecture 6 – File Access and Arrays Today’s topic is reading and writing external files. This is a common and important task in programming that often uses “do” loops and arrays to read or write a lot of information with just a few lines of code. Here’s an example: program files dimension n(10) open(unit=5,file='input.txt',status='unknown') do i=1,10 read(5,*) n(i) write(*,*) n(i) end do close(5) write(*,*) 'Input file successfully read.' open(unit=6,file='output.txt',status='unknown') do i=1,10 write(6,*) n(i)**5 end do close(6) stop end This program requires the input.txt file, which is available on the class website, to be in the same folder as the program. The input file simply contains the numbers 1 through 10, one per line. The “open” command is used to open (or create) an external file. Fortran can only read and write plain text to files, regardless of the filename extension. If you try to make Fortran read from a more complex file type like a Word .doc file or an image .jpg file, the file’s content will be converted to binary and you’ll get a lot of gibberish. The unit of the file being opened is simply how you will refer to it in the program; it’s a shorthand identifier so you don’t have to type the complete filename every time. You can even use “if” statements to open one of several possible files and then use the same unit and commands no matter which file was selected. So, while this system is quite different from how files are handled in most other languages, it does have some advantages. The filename part points to a file in the same directory as the Fortran program, and it
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/21/2011 for the course AERO 3970 taught by Professor Doucette during the Spring '11 term at Auburn University.

Page1 / 3

Fortran_Lecture_6 - Fortran Class Notes Lecture 6 File...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online