Fortran Input .txt file into Fortran program

AI Thread Summary
The discussion centers on reading a text file containing a 9x2 table of integers into a Fortran program. The initial attempt incorrectly used a WRITE statement instead of a READ statement, resulting in random numbers being displayed. Key corrections include removing unnecessary declarations and ensuring the READ statement is correctly formatted to read from the file associated with unit 20. The correct approach involves using a READ statement like "READ (20,*) input(I,J)" within the loop to populate the integer array from the text file. Additionally, it is emphasized that the text file must exist in the same directory as the executable for successful reading. Links to external resources on Fortran file I/O are provided for further assistance.
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?
 
Technology news on Phys.org
You need a 'read' statement that reads from unit 20. :smile:
 
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
 

Similar threads

Replies
8
Views
1K
Replies
5
Views
2K
Replies
12
Views
3K
Replies
5
Views
5K
Replies
41
Views
5K
Replies
4
Views
2K
Replies
17
Views
6K
Replies
1
Views
3K
Back
Top