Input .txt file into Fortran program

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
9 replies · 12K views
ae62589
Messages
3
Reaction score
0
I am trying to input a txt file into my fortran program. My txt file is a 9x2 table of integers, all I want is to display the values in the same format in my program. I tried using,

program name
implicit none

integer :: input(9,2), I, J

open (unit=20, file="values.txt")
DO I = 1, 9
DO J = 1, 2
WRITE(*,*) input(I,J)
END DO
END DO
close (unit=20)

end program name

But when I run the program, it comes out with a bunch of random numbers. How can I get it to display my table just the way it is?
 
Physics news on Phys.org
It still does the same thing, except for now you can input one value before the program displays a random line of numbers. I added a read statement right after I opened the txt file, and added unit20 to the declaration of integers.
 
Show us your code. Your first program had a gross error - you were intending to get input from a file, but were writing to it, not reading from it.

You should not declare unit20. The line
Code:
open(unit = 20, file="values.txt")
associates a unit number with a text file, and subsequent read operations pull data out of it. The file values.txt needs to exist before the program runs, and needs to be in the same directory as the executable.
 
My program and my txt file are in the same directory. I am trying to extract everything from the text file, not just one value. Does that correspond with the unit = 20?

program name
implicit none

integer :: input(9,2), I, J
character :: unit20

open (unit=20, file="values.txt")
READ (*,*) unit20
DO I = 1, 9
DO J = 1, 2
WRITE(*,*) input(I,J)
END DO
END DO
close (unit=20)

end program name
 
ae62589 said:
My program and my txt file are in the same directory. I am trying to extract everything from the text file, not just one value. Does that correspond with the unit = 20?

program name
implicit none

integer :: input(9,2), I, J
character :: unit20 <<< get rid of this line

open (unit=20, file="values.txt")
READ (*,*) unit20 <<< get rid of this line
DO I = 1, 9
DO J = 1, 2
WRITE(*,*) input(I,J)
END DO
END DO
close (unit=20)

end program name

You are still missing the main point. Your first READ statement attempts to read a character from the file. Your loop should be reading from the file, not writing to it.
 
Mark44 said:
Your first READ statement attempts to read a character from the file.

Actually, the statement

READ (*,*) unit20

reads a single character from the default input (usually the keyboard) according to the default format, and stores it in the variable 'unit20'. It reads a single character because 'unit20' is declared as containing a single character.

To read from unit 20 (i.e. your file input.txt), you need a READ statement like

READ (20,*) something
 
jtbell said:
Actually, the statement

READ (*,*) unit20

reads a single character from the default input (usually the keyboard) according to the default format, and stores it in the variable 'unit20'.
Right. That went right by me.
 
I am trying to input a txt file into my fortran program. My txt file contains 1 column 190 rows of integers, please tell me how i can insert it into my fortran program