How to Read Characters in FORTRAN
Long before Microsoft Windows existed, computer programmers used a language called FORTRAN to solve complex scientific and engineering problems. Develop starting in 1953, this aging language can also perform simple tasks such as reading and manipulating characters. You might find this ability useful when prompting users for nonnumeric information or reading text strings from external files. Because FORTRAN handles input operations automatically, you only need to enter the right commands to perform these tasks.
Instructions
-
Read from Keyboard
-
1
Open one of your FORTRAN files and add the following code after your program statement:
character*4 x
character*2 yThis code defines two character variables whose lengths are 4 and 2, respectively. Use this format when defining character variables.
-
2
Add the code shown below after those two variable declarations:
print *, "Please enter a four-character word and a two-character word"
read (*,1) x, y
1 format (A4, A2)The read statement reads data into the x and y variables. It also contains two parameters surrounded by parenthesis. The first parameter, *, tells FORTRAN to accept input from the keyboard. The second parameter, 1, is a format number. It points to a label in front of a statement that contains formatting instructions. That statement is the format statement shown after the read statement. The format statement contains A4 and A2. The letter A is a format code that tells FORTRAN to treat data as a text string. Each formatting code in the parameter list corresponds to a variable in the read statement. In this example, the computer reads the first four characters that you type and store them in the x variable. It then stores the next two characters that you type in the y variable. The print statement displays a text string that helps users understand what you would like them to enter.
-
-
3
Save the program and run it as you normally do. When a command window opens, type "aaaabb" -- without the quotes -- into the window and press "Enter." The code reads the characters and assign "aaaa" to the x variable and "bb" to the y variable.
Read from File
-
4
Open Notepad and paste the text shown below into a new document and save it.
dddd ee
-
5
Open another Fortran file and paste these variable declarations below the program statement:
character*4 x
character*2 y -
6
Add the following code after the code shown in the previous step:
open(2, FILE="?")
read(2, 3) x, y
3 format(A4, 1x, A2)
close (2)Replace the question mark with the name of the text file you saved. The open statement opens that file and assigns a unit number to it. That number is 2. The read statement appears different this time. Its second parameter, 3, refers to the format statement below it, but the first parameter refers to 2, the file number associated with your open statement. Because these numbers match, the read statement will read data from that file and store it in the x and y variables.
-
7
Save the program and run it. The program reads the characters from your file and stores them in the x and y variables.
-
1
Tips & Warnings
The file you created contains one space between "aaaa" and "bb." FORTRAN knows that a space is there because the format statement contains 1x. This value tells it to skip one space after reading data into the first variable. Change "1x" to another value if more spaces exist between the values in your input file. If four spaces separate values, for example, change 1x to 4x.
References
- Photo Credit Comstock/Comstock/Getty Images