Fortran Can't read float number from file

  • Thread starter Thread starter bou
  • Start date Start date
  • Tags Tags
    File Float
AI Thread Summary
The discussion centers on issues with reading float numbers from a file in Fortran, specifically encountering a runtime error due to invalid characters in the input format. The original code works for integers but fails with real numbers when using a specific format. A suggestion is made to avoid using equality checks with floating-point numbers due to potential rounding errors, recommending a double loop to modify the values in the array. The user also seeks help with converting the input file format without using Excel, while still facing challenges in changing specific data values. The conversation highlights the importance of correctly handling floating-point comparisons and file formatting in Fortran.
bou
Messages
3
Reaction score
0
Hi everyone I need help for reading file that contain float number, I don't 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

Technology news on Phys.org
Your text file contains tabs. I believe that is the problem.
 
  • Like
Likes bou
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
 
bou said:
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
DrClaude said:
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.
 
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top