Input .txt file into Fortran program

Click For Summary

Discussion Overview

The discussion revolves around the challenges of reading a .txt file into a Fortran program, specifically focusing on inputting a table of integers and displaying them correctly. Participants are addressing issues related to file handling, reading data, and the correct use of Fortran syntax.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes their attempt to read a 9x2 table of integers from a text file but encounters issues with displaying random numbers instead of the intended values.
  • Another participant suggests the need for a 'read' statement to properly read from the file.
  • A participant points out that the initial code mistakenly writes to the file instead of reading from it, indicating a misunderstanding of file operations.
  • There is a suggestion to remove unnecessary declarations and clarify the purpose of the unit number associated with the file.
  • Participants discuss the correct syntax for reading from the file, emphasizing that the read statement should reference the file unit number rather than default input.
  • One participant raises a new question about reading a different format of data (1 column, 190 rows) from a text file, seeking guidance on how to implement this in Fortran.
  • Links to external resources are provided by a participant, potentially offering additional guidance on Fortran input/output operations.

Areas of Agreement / Disagreement

Participants generally agree on the need for a proper read statement to extract data from the file, but there is ongoing confusion regarding the correct implementation and syntax. The discussion remains unresolved as participants continue to clarify and refine their understanding of file input in Fortran.

Contextual Notes

There are limitations in the discussion regarding the assumptions about file formats, the need for the file to exist in the correct directory, and the specifics of how to structure read statements for different data formats.

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 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 41 ·
2
Replies
41
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 17 ·
Replies
17
Views
7K
  • · Replies 1 ·
Replies
1
Views
4K