0

I want to read and store the data from the csv file in Fortran but the stored data does not have the same decimal place and digits as the original data. I want to read and store the same value which will be used in calculation in other programs.

The csv file is as follows:
csv data

However, the output looks like this:

A1 1.0000000000000001E-017

A2 6.9999999999999997E-018

A3 3.5999999999999998E-016

A4 1.6000000000000001E-017

A5 8.9999999999999999E-018

Below is my program.

PROGRAM read_csv
  IMPLICIT NONE
  CHARACTER(LEN=20) :: header(5)
  REAL(kind=8) :: number(5)
  INTEGER :: i, status
  CHARACTER(LEN=50) :: line
  CHARACTER(LEN=20) :: filename
  INTEGER :: unit

  ! Open the file
  OPEN(UNIT=unit, FILE='data.csv', STATUS='OLD')

  ! Read the header line and ignore it
  READ(unit, *) line

  ! Read the data and store it in variables
  DO i = 1, 5
     READ(unit, *, IOSTAT=status) header(i), number(i)
     IF (status /= 0) THEN
        WRITE(*,*) "Error reading data from the file."
        STOP
     END IF
  END DO

  ! Close the file
  CLOSE(unit)

  ! Print the data to verify it
  DO i = 1, 5
     WRITE(*,*) header(i), number(i)
  END DO

END PROGRAM read_csv

I tried with the above code but it didn't provide me with the result I was looking for. I want the stored variables should have the same values as it is present in the csv file.

Mark
  • 35
  • 4

0 Answers0