Can't read float number from file

  • Fortran
  • Thread starter bou
  • Start date
  • Tags
    File Float
In summary, the conversation is about a problem with reading a text file containing float numbers in a FORTRAN code. The code works fine with integer numbers, but when using real numbers, it gives a Runtime Error due to an invalid character. The solution suggested is to convert the input file to a format that FORTRAN can read, but there is an issue with changing the data values. The expert recommends not using equalities with floating-point numbers and suggests using a double loop to change the values of Q instead of using an IF statement.
  • #1
bou
3
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

  • 100y_dem2.txt
    2.8 MB · Views: 509
Technology news on Phys.org
  • #2
Your text file contains tabs. I believe that is the problem.
 
  • Like
Likes bou
  • #3
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
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
  • #5
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.
 
  • #6
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.
 

FAQ: Can't read float number from file

What is a float number?

A float number, also known as a floating-point number, is a numerical value with a decimal point. It can represent both whole and fractional numbers and is commonly used in scientific and engineering calculations.

Why can't I read a float number from a file?

There could be several reasons why you are unable to read a float number from a file. It could be due to incorrect file formatting, an issue with the code used to read the file, or a problem with the file itself. It is important to carefully check the file and code to identify the exact cause of the issue.

How can I fix the issue of not being able to read a float number from a file?

The solution to this problem will depend on the specific cause. If there is an issue with the file formatting, you can try adjusting the code to read the file accordingly. If there is an error in the code, you may need to debug and fix it. If the file itself is corrupted, you may need to obtain a new version of the file.

What are some common file formatting errors that can affect reading float numbers?

Some common file formatting errors that can affect reading float numbers include using incorrect delimiters, not following the correct data structure, or using the wrong data type for the values. It is important to carefully review the file formatting guidelines and ensure that the data is saved in the correct format.

Can I convert a string to a float number when reading from a file?

Yes, it is possible to convert a string to a float number when reading from a file. This can be done using a type conversion function or method, depending on the programming language being used. However, it is important to ensure that the string contains a valid numerical value, otherwise the conversion may result in an error.

Similar threads

Replies
4
Views
2K
Replies
12
Views
2K
Replies
1
Views
3K
Replies
5
Views
4K
Replies
2
Views
1K
Replies
5
Views
1K
Replies
25
Views
2K
Back
Top