Fortran: reading the data from file

In summary: This statement reads one line and does nothing with the data.In summary, the conversation discusses the use of Fortran to read data from a file into a variable for memory calculation. The speaker also asks about any notices or tips for reading data into variables, mentioning examples found through Google and a tutorial on Fortran file access. They also mention the use of netcdf APIs for reading and writing to certain types of files. Finally, the speaker asks about creating a matrix coeff A from the data in the file and asks for an example of skipping a line while reading data from a file.
  • #1
Huyen401
171
0
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)
 
Technology news on Phys.org
  • #2
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.
 
  • Like
Likes Huyen401
  • #3
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?
 
  • #4
Huyen401 said:
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:
  • #5
Mark44 said:
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 don't 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:
  • #6
Huyen401 said:
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?
 
  • Like
Likes Huyen401
  • #7
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
Mark44 said:
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
https://drive.google.com/file/d/0BwK2cNv1sXawbWx6N21LQXMtME0/view?usp=sharing
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
    1.jpg
    51 KB · Views: 523
Last edited by a moderator:
  • #8
Huyen401 said:
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, *)
 
  • Like
Likes Huyen401

What is Fortran?

Fortran is a high-level programming language used primarily for scientific and engineering applications. It stands for "Formula Translation" and was developed in the 1950s.

How do I read data from a file in Fortran?

To read data from a file in Fortran, you will need to use the READ statement, which allows you to specify the input format and the variables to store the data in. You will also need to open the file using the OPEN statement and close it using the CLOSE statement.

What is the format for reading data from a file in Fortran?

The format for reading data from a file in Fortran is specified in the READ statement using format descriptors. These descriptors indicate the data type and length of the input, as well as any formatting options. They are enclosed in parentheses and separated by commas.

Can I read data from multiple files in Fortran?

Yes, you can read data from multiple files in Fortran by opening each file separately and using a READ statement for each file. You can also use a DO loop to read data from multiple files in a more efficient manner.

What happens if there is an error while reading data from a file in Fortran?

If an error occurs while reading data from a file in Fortran, the program will terminate and an error message will be displayed. It is important to check for potential errors, such as end-of-file or incorrect data format, and handle them appropriately to avoid program crashes.

Similar threads

  • Programming and Computer Science
Replies
2
Views
919
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
1
Views
562
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
1
Views
287
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
21
Views
559
  • Programming and Computer Science
Replies
11
Views
998
  • Programming and Computer Science
Replies
33
Views
2K
  • Programming and Computer Science
Replies
14
Views
655
Back
Top