Microsoft KB Archive/120397

= Use atof or atoi Functions to Convert from Char Strings =

Article ID: 120397

Article Last Modified on 11/18/2003

-

APPLIES TO


 * Microsoft Fortran PowerStation 1.0a for MS-DOS
 * Microsoft FORTRAN PowerStation 32
 * Microsoft FORTRAN PowerStation 32

-



This article was previously published under Q120397



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

Additional query words: kbinf 1.00 1.00a 4.00 VAX porting port

Keywords: kblangfortran kbcode KB120397

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.