Use atof or atoi Functions to Convert from Char Strings

Zavrnitev odgovornosti za umaknjeno vsebino KB

Ta članek govori o izdelkih, za katere Microsoft ne ponuja več podpore. Zato je ta članek na voljo »tak, kot je«, in ne bo več posodobljen.

Summary

Microsoft FORTRAN PowerStation does not permit list-directed I/O for internal files. As a result, you may find it difficult to convert character strings to numeric values unless you know the exact format of the string. However, the atoi and atof functions included in the LIBC.LIB library can accomplish the translation.

More Information

To use the atof and atoi functions, you must create interfaces to the functions, and declare the types of the functions.


The LIBC.LIB library containing the atof and atoi functions, is linked by default when building FORTRAN programs in PowerStation, so no special options are required.


The atof function expects the character string to be in the following form:


[whitespace] [sign] [digits] [.digits] [ {d | D | e | E }[sign]digits]
Use some caution and error checking when employing these functions. If the atof or atoi function cannot convert the string format, it returns zero (0.0 or 0) as the error condition. You should consult the C run-time library reference for complete information on these functions.


FORTRAN does not null terminate strings, so the functions may incorrectly translate a string that contains digits up to the end. To be safe, insert a null character in the last position of the string. For example, the following command null terminates the string "a":
      CHARACTER*10 a
a(10:10) = char(0)

Sample Code

Compiler options needed: none
C Interface to the atof function which is in the LIBC library

INTERFACE to REAL*8 FUNCTION atof[C](a)
CHARACTER*(*) a[reference]
END
C Interface to the atoi function which is in the LIBC library

INTERFACE to INTEGER FUNCTION atoi[C](a)
CHARACTER*(*) a[reference]
END
REAL*8 x
REAL*8 atof !prevents error F2236: ATOF : length redefined
INTEGER i
INTEGER atoi !prevents error F2201: ATOI : type redefined
CHARACTER*30 a
PRINT *, "Type a numeric value"
READ '(A)', a
a(30:30) = char(0)
x = atof(a)
i = atoi(a)
PRINT *, '"',a,'"', x, i
END
Lastnosti

ID članka: 120397 – Zadnji pregled: 18. nov. 2003 – Revizija: 1

Povratne informacije