Input .txt file into Fortran program

In summary, to input a txt file into a Fortran program, you can use the 'open' and 'read' statements to associate a unit number with the file and read the data from the file into your program. Make sure the file is in the same directory as your program and that the data is formatted correctly for Fortran to read.
  • #1
ae62589
3
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
  • #2
You need a 'read' statement that reads from unit 20. :smile:
 
  • #3
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.
 
  • #4
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.
 
  • #5
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
 
  • #6
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.
 
  • #7
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
 
  • #8
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.
 
  • #9
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
 

1. How do I input a .txt file into a Fortran program?

To input a .txt file into a Fortran program, you first need to declare an input file unit and open the file using the open statement. Then, you can use the read statement to read the data from the file into your program. Don't forget to close the file using the close statement once you are done.

2. What format should my .txt file be in for it to be read by a Fortran program?

The format of your .txt file will depend on how you plan to read the data in your Fortran program. For example, if you are using the read statement with a format specification, then your file should be formatted according to the specified format. If you are using the read statement with list-directed input, then you can use any valid format for your data.

3. Can I use a .txt file for both input and output in my Fortran program?

Yes, you can use a .txt file for both input and output in your Fortran program. You will just need to declare and open the file twice, once for input and once for output. Be sure to use different file units for each.

4. How do I handle errors when reading a .txt file in a Fortran program?

You can use the ioerror specifier in your read statement to handle any errors that may occur while reading the file. This will allow you to check for errors and handle them appropriately in your program.

5. Are there any limitations on the size of the .txt file I can input into my Fortran program?

The size of the .txt file you can input into your Fortran program will depend on the memory and storage capacity of your system. However, it is always a good idea to check the file size before attempting to read it in, and to optimize your program for efficient memory usage.

Similar threads

  • Programming and Computer Science
Replies
4
Views
570
  • Programming and Computer Science
2
Replies
41
Views
3K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
17
Views
4K
Back
Top