Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran Can't read float number from file

  1. Dec 21, 2016 #1

    bou

    User Avatar

    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
    Code (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
     

    Attached Files:

  2. jcsd
  3. Dec 21, 2016 #2
    Your text file contains tabs. I believe that is the problem.
     
  4. Dec 22, 2016 #3

    bou

    User Avatar

    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
    Code (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
    Code (Fortran):
    IF(Q(I,J) .EQ. -9999.) Q(I,J)= 9999.
    any suggestion?
    thank you in advance
     
  5. Dec 22, 2016 #4

    DrClaude

    User Avatar

    Staff: Mentor

    Don't use equalities with floating-point numbers. You have to allow for rounding errors.
     
  6. Dec 22, 2016 #5

    bou

    User Avatar

    thank you for your reply, i did try .LT. -1 but also same reuslt -9999 value not change.
     
  7. Dec 22, 2016 #6

    DrClaude

    User Avatar

    Staff: Mentor

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted