To read a binary file using fortran

In summary, the conversation is about reading a binary file using Fortran and the output format not being as desired. The program currently reads the data into one array and prints it out without sorting. Suggestions are given to use multiple arrays for the data and to add calculation statements to the program.
  • #1
Rems
4
0
I am trying to read a binary file using fortran.
this is how the binary file looks like.

1901 109.1000 241.4000 284.2000 121.9000

1902 104.0000 283.7000 202.6000 201.9000

1903 114.8000 293.0000 279.6000 204.4000

1904 158.8000 266.4000 210.4000 129.6000

1905 88.70000 252.5000 202.6000 174.6000

1906 176.9000 285.8000 259.8000 179.8000

1907 150.1000 223.5000 311.6000 95.10000

1908 125.4000 331.5000 317.0000 153.8000 and so on

This is my code

PROGRAM read_data
IMPLICIT NONE

integer ::i,j
REAl , dimension(5,110)::a

!a(1,*)=year
!a(2,*)=june
!a(3,*)=july
!a(4,*)=august
!a(5,*)=september

open (55,file='data_ISMR',status='old')
open (66,file='output4.txt',status='new')

read (55,*) a
write(66,*) a

close(55)
END PROGRAM read_data
~
There is no error in the program. But result is not displayed properly. this is how it looks like

1901.000 109.1000 241.4000 284.2000 121.9000 1902.000 104.0000 283.7000 202.6000 201.9000 1903.000 114.8000 293.0000 279.6000 204.4000 1904.000 158.8000 266.4000 210.4000 129.6000 1905.000 88.70000 252.5000 202.6000 174.6000 1906.000 176.9000 285.8000 259.8000 179.8000 1907.000 150.1000 223.5000 311.6000 95.10000 1908.000 125.4000 331.5000 317.0000 153.8000 1909.000 205.0000 and so on

any suggestions how to improve the program?
also i want to find the sum of june,july,august,september for each year and add it all together from year 1901 to 2010.
i am new to fortran. anybody having any suggestions?
 
Technology news on Phys.org
  • #2
But result is not displayed properly.

What do you want it to look like?
 
  • #3
I'm puzzled as to why you say you want to read a binary file and then you display what seems to clearly be a text file.
 
  • #4
1. If you want the output from the file to look different, then you need to decide on what format you desire and use FORMAT statements with WRITE statements, instead of relying on the default output format.

2. As your program now stands, you read in your data into one big array and then print out the big array, without doing any sorting of the data. Have you considered using more than one array, say an array for the Year, and similar arrays for the monthly data (whatever it may be)? With proper programming, Fortran can be quite versatile at reading and sorting your data.

3. If you want to do calculations on the data after it is read, then you will have to add the proper calculation statements to your program. This will be easier if you look at comment 2 above.
 
  • Like
Likes 1 person
  • #5
phinds said:
I'm puzzled as to why you say you want to read a binary file and then you display what seems to clearly be a text file.

sorry. that was a mistake. its text file. i want to open the file using fortran and do the calculations
 

FAQ: To read a binary file using fortran

How do I open and read a binary file in Fortran?

To open and read a binary file in Fortran, you will need to use the OPEN statement and specify the FORM='BINARY' option. This will allow you to access the binary data in the file. You will also need to specify the ACCESS='SEQUENTIAL' option if you want to read the file sequentially. For example, the code OPEN(1,FILE='file.bin',FORM='BINARY',ACCESS='SEQUENTIAL') will open the file file.bin for sequential binary access.

How do I read data from a binary file in Fortran?

To read data from a binary file in Fortran, you will need to use the READ statement. This statement takes in the unit number of the open file, the variables you want to read the data into, and the number of data items to be read. For example, the code READ(1,*) variable1, variable2, variable3 will read three data items from the file with unit number 1 into the variables variable1, variable2, and variable3.

How do I handle errors while reading a binary file in Fortran?

To handle errors while reading a binary file in Fortran, you can use the IOSTAT parameter in the READ statement. This will return a specific error code if there is an error while reading the file. You can then use an IF statement to check for this error code and handle it accordingly. For example, the code READ(1,*,IOSTAT=ierr) variable will read data into variable and store the error code in the variable ierr.

How do I know the size of my binary file in Fortran?

To determine the size of a binary file in Fortran, you can use the INQUIRE statement. This statement takes in the FILESIZE option and returns the size of the file in bytes. For example, the code INQUIRE(FILE='file.bin', FILESIZE=file_size) will store the size of file.bin in the variable file_size.

How do I close a binary file in Fortran?

To close a binary file in Fortran, you can use the CLOSE statement. This statement takes in the unit number of the file you want to close. For example, the code CLOSE(1) will close the file with unit number 1.

Similar threads

Replies
12
Views
2K
Replies
5
Views
4K
Replies
5
Views
2K
Replies
1
Views
3K
Replies
5
Views
1K
Replies
1
Views
2K
Replies
2
Views
1K
Replies
1
Views
1K
Replies
19
Views
6K
Back
Top