Sun Studio 12:Fortran 库参考

1.4.16 getcfgetc:获取下一个字符

getcfgetc 从输入流中获取下一个字符。请勿将这些例程的调用与相同逻辑单元中进行的正常 Fortran I/O 混在一起。

1.4.16.1 getc:从 stdin 中获取下一个字符

该函数的调用方式如下所示:

INTEGER*4 getc

status = getc( char )

char

字符

输出 

下一个字符 

返回值 

INTEGER*4

输出 

status=0:OK

status=-1:文件结束

status>0:系统错误代码或 f95 I/O 错误代码

示例:使用 getc 获取从键盘输入的每个字符;请注意 Ctrl-D (^D):


       character char
       INTEGER*4 getc, status
       status = 0
       do while ( status .eq. 0 )
         status = getc( char )
         write(*, ’(i3, o4.3)’) status, char
       end do
       end

编译之后,运行以上源代码的样例如下:


demo% a.out
ab            Program reads letters typed in
0 141         Program outputs status and octal value of the characters entered
0 142            141 represents ’a’, 142 is ’b’
0 012            012 represents the RETURN key
^D               terminated by a CONTROL-D.
-1 377        Next attempt to read returns CONTROL-D
demo%

对于逻辑单元,请勿将正常的 Fortran 输入与 getc() 混在一起。

1.4.16.2 fgetc:从指定逻辑单元中获取下一个字符

该函数的调用方式如下所示:

INTEGER*4 fgetc

status = fgetc( lunit, char )

lunit

INTEGER*4

输入 

逻辑单元 

char

字符

输出 

下一个字符 

返回值 

INTEGER*4

输出 

status=-1:文件结束

status>0:系统错误代码或 f95 I/O 错误代码

示例:使用 fgetctfgetc.data 中获取每个字符;请注意换行 (Octal 012):


       character char
       INTEGER*4 fgetc, status
       open( unit=1, file=’tfgetc.data’ )
       status = 0
       do while ( status .eq. 0 )
          status = fgetc( 1, char )
          write(*, ’(i3, o4.3)’) status, char
       end do
       end

编译之后,运行以上源代码的样例如下:


demo% cat tfgetc.data
ab
yz
demo% a.out
0 141       ”a’ read
0 142       ”b’ read
0 012       linefeed read
0 171       ”y’ read
0 172       ”z’ read
0 012       linefeed read
-1 012      CONTROL-D read
demo%

对于逻辑单元,请勿将正常的 Fortran 输入与 fgetc() 混在一起。

另请参见:getc(3S)、intro(2) 和 perror(3F)。