_getw_getw

从流中获取整数。Gets an integer from a stream.

语法Syntax

int _getw(
   FILE *stream
);

参数Parameters

streamstream
指向文件结构的指针。Pointer to the FILE structure.

返回值Return Value

_getw返回读取的整数值。_getw returns the integer value read. EOF的返回值指示错误或文件尾。A return value of EOF indicates either an error or end of file. 但是,因为EOF值也是合法的整数值,请使用feofferror来验证文件尾或错误条件。However, because the EOF value is also a legitimate integer value, use feof or ferror to verify an end-of-file or error condition. 如果streamNULL,则会调用无效参数处理程序,如参数验证中所述。If stream is NULL, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,则将errno设置为EINVAL ,并且该函数将返回EOFIf execution is allowed to continue, errno is set to EINVAL and the function returns EOF.

备注Remarks

_Getw函数从与关联的文件中读取int类型的下一个二进制值,并递增关联的文件指针(如果有)以指向下一个未读字符。The _getw function reads the next binary value of type int from the file associated with stream and increments the associated file pointer (if there is one) to point to the next unread character. _getw不假定流中项的任何特殊对齐方式。_getw does not assume any special alignment of items in the stream. 由于 int 类型的int类型的大小和int类型中的字节顺序在不同系统之间不同,因此可能会出现与 _getw有关的问题。Problems with porting can occur with _getw because the size of the int type and the ordering of bytes within the int type differ across systems.

要求Requirements

例程所返回的值Routine 必需的标头Required header
_getw_getw <stdio.h><stdio.h>

有关更多兼容性信息,请参阅 兼容性For more compatibility information, see Compatibility.

示例Example

// crt_getw.c
// This program uses _getw to read a word
// from a stream, then performs an error check.

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
   FILE *stream;
   int i;

   if( fopen_s( &stream, "crt_getw.txt", "rb" ) )
      printf( "Couldn't open file\n" );
   else
   {
      // Read a word from the stream:
      i = _getw( stream );

      // If there is an error...
      if( ferror( stream ) )
      {
         printf( "_getw failed\n" );
         clearerr_s( stream );
      }
      else
         printf( "First data word in file: 0x%.4x\n", i );
      fclose( stream );
   }
}

输入:crt_getw.txtInput: crt_getw.txt

Line one.
Line two.

OutputOutput

First data word in file: 0x656e694c

请参阅See also

流 I/OStream I/O
_putw_putw