# Fortran: reading the data from file

• Fortran
Hello, I have data in file and I want to read the data into variable in fortran to save memory caculation. I want to know: When I open file inputdata, whether fortran have any notices about the way it read data into variables? (like mathematica: each open file, we just read in the order anyway)

## Answers and Replies

jedishrfu
Mentor
You can find examples via google.

Here's a tutorial on it:

http://www.math.hawaii.edu/~hile/fortran/fort7.htm

It also depends on the type of input file like if its a text file or a binary file file access would be somewhat different
or if it was a netcdf file where you'd need to use netcdf apis to read/write to it.

Huyen401
Thanks. In case, I have the data file like:
1 2 3 4 5
6 7 8 9 10
4 5 1 5 8
9 6 3 2 1
7 4 2 1 13
and I want to create the coeff A of equations system
A= 1 2 3 5
4 5 1 8
9 6 3 1
Whether or not I can take these data from the file by statement?

Mark44
Mentor
Thanks. In case, I have the data file like:
1 2 3 4 5
6 7 8 9 10
4 5 1 5 8
9 6 3 2 1
7 4 2 1 13
and I want to create the coeff A of equations system
A= 1 2 3 5
4 5 1 8
9 6 3 1
Whether or not I can take these data from the file by statement?
Yes, this is pretty straightforward to do. What have you tried so far? You shouldn't expect us to write the code for you, though.

Last edited:
Yes, this is pretty straightforward to do. What have you tried so far? You shouldn't expect us to write the code for you, though.
No. I mean I am a new one in programing. So many concept in fortran I dont Know.
I just want to know whether it can do that or not and How to do that. In my code, [A]= [1]+[2]+[3]+[4] so many matrices so that I try to find more about reading array data from file on webs.

Last edited by a moderator:
Mark44
Mentor
Thanks. In case, I have the data file like:
1 2 3 4 5
6 7 8 9 10
4 5 1 5 8
9 6 3 2 1
7 4 2 1 13
and I want to create the coeff A of equations system
A= 1 2 3 5
4 5 1 8
9 6 3 1
Whether or not I can take these data from the file by statement?
It's not clear to me what you're trying to do. From what you show above, the pattern appears to be
1. Read in the contents of a row, and store the 1st, 2nd, 3rd, and 5th item in the array, skipping the 4th item.
2. Skip the next row.
3. Repeat steps 1 and 2.

However, the numbers you show in your array A don't follow this pattern when you get to the 4th row. Is that a mistake?

Huyen401
I want to solve the equation system by fortran and Laplack subroutine. And the first thing to do is create the matrix coeff A. I think
It's not clear to me what you're trying to do. From what you show above, the pattern appears to be
1. Read in the contents of a row, and store the 1st, 2nd, 3rd, and 5th item in the array, skipping the 4th item.
2. Skip the next row.
3. Repeat steps 1 and 2.

However, the numbers you show in your array A don't follow this pattern when you get to the 4th row. Is that a mistake?
Thank. To be clear, my problem is solve equations system
By the way, Can you give me a example about statement: skip the row while reading data from file and read the any rows from input file?
Thanks a lot.

#### Attachments

• 1.jpg
51.6 KB · Views: 424
Last edited by a moderator:
jtbell
Mentor
Can you give me a example about statement: skip the row while reading data from file

A read() statement normally reads one line from the file. To skip a line, simply use a read() statement that does not list any input variables:

Fortran:
      read (ifile, *)

Huyen401