Can't read float number from file

  • Fortran
  • Thread starter bou
  • Start date
  • #1
bou
3
0

Main Question or Discussion Point

Hi everyone I need help for reading file that contain float number, I dont understand the problem or what should I have to fix; the code seem to be fine if the read file is integer number but if the number is real number the FORTRAN give me error Runtime Error: debug_input.f(12): Invalid character in real input field
Fortran:
      PROGRAM test
      PARAMETER (NX=928,NY=775)
     
      REAL IFDY (NX,NY)
      DO J=1,NY
        DO I=1,NX
          IFDY(I,J)=0
          ENDDO
      ENDDO    
      open(100, FILE="100y_dem2.txt",
     & STATUS='OLD', ACTION='READ',RECL=25000)
      read(100,'(928f7.4)')((IFDY(I,J),I=1,NX),J=1,NY)
      close(100)
      PRINT *, ifdy
      END PROGRAM
the code can read file if i change from format(928f7.4) to default (*) but i need to use format to read file for calculate, sorry for my poor explanation.
thank in advance
 

Attachments

Answers and Replies

  • #2
.Scott
Homework Helper
2,548
916
Your text file contains tabs. I believe that is the problem.
 
  • Like
Likes bou
  • #3
bou
3
0
thank you for your answer, now i try to covert the input file format to the format that fortran can read without use excel , but i have a problem with changing the data value, can you help me have a look
Fortran:
      PROGRAM CONVERT
      PARAMETER (IG=775,JG=928)
      INTEGER I,J
      REAL Q(775,928)
      CHARACTER FN*40, FN2*3, FNOUT*45, ANS*1
  200 CONTINUE
      WRITE(6,*) 'INPUT FILE NAME'
      READ(5,*) FN
      FN2="new"
      FNOUT=FN2//FN
      OPEN(2,FILE=FN,STATUS='OLD',RECL=25000)
      DO J=1,JG
       READ(2,*)(Q(I,J),I=1,IG)
      ENDDO
      CLOSE(2)
      OPEN(10,FILE=FNOUT, RECL=25000)
      DO J=1,JG
       IF(Q(I,J) .EQ. -9999.) Q(I,J)= 9999.
        WRITE(10, '(775F8.1)')(Q(I,J),I=1,IG)
       ENDDO
      CLOSE(10)
     
      WRITE(6,*)' ANOTHER DATA ? (Y/N)'
      READ(5,*) ANS
      IF(ANS .EQ. 'Y') GOTO 200
     
      STOP
      END
it seem this line not work, the output file still have -9999 value
Fortran:
IF(Q(I,J) .EQ. -9999.) Q(I,J)= 9999.
any suggestion?
thank you in advance
 
  • #4
DrClaude
Mentor
7,341
3,527
it seem this line not work, the output file still have -9999 value
Fortran:
IF(Q(I,J) .EQ. -9999.) Q(I,J)= 9999.
any suggestion?
Don't use equalities with floating-point numbers. You have to allow for rounding errors.
 
  • Like
Likes jim mcnamara and bou
  • #5
bou
3
0
Don't use equalities with floating-point numbers. You have to allow for rounding errors.
thank you for your reply, i did try .LT. -1 but also same reuslt -9999 value not change.
 
  • #6
DrClaude
Mentor
7,341
3,527
I just realized that you are not looping over I. You will have to take the IF out of the print loop and have a double loop to change the values of Q.
 

Related Threads on Can't read float number from file

  • Last Post
Replies
3
Views
2K
Replies
2
Views
2K
  • Last Post
Replies
3
Views
4K
Replies
5
Views
4K
Replies
5
Views
17K
Replies
4
Views
10K
Replies
7
Views
1K
Replies
2
Views
29K
Top