Visual C++ 2015 express:_stat不能在Windows XP上运行

Tob*_*ann 7 c++ winapi visual-studio visual-c++ visual-studio-2015

使用as (目标Win32)使用Visual C++ 2015 Express编译的MSDN运行以下示例的_stat在Windows 7上正常运行,但在我测试的几台计算机上不在Windows XP上运行.v140_xpPlatform Toolset

// crt_stat.c
// This program uses the _stat function to
// report information about the file named crt_stat.c.

#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>

int main()
{
  struct _stat buf;
  int result;
  char timebuf[26];
  char* filename = "crt_stat.c"; // Absolute paths like "D:\\crt_stat.c" produce the same behaviour.
  errno_t err;

  // Get data associated with "crt_stat.c":
  result = _stat( filename, &buf );

  // Check if statistics are valid:
  if ( result != 0 )
  {
    perror( "Problem getting information" );
    switch ( errno )
    {
    case ENOENT:
      printf( "File %s not found.\n", filename );
      break;
    case EINVAL:
      printf( "Invalid parameter to _stat.\n" );
      break;
    default:
      /* Should never be reached. */
      printf( "Unexpected error in _stat.\n" );
    }
  }
  else
  {
    // Output some of the statistics:
    printf( "File size     : %ld\n", buf.st_size );
    printf( "Drive         : %c:\n", buf.st_dev + 'A' );
    err = ctime_s( timebuf, 26, &buf.st_mtime );
    if ( err )
    {
      printf( "Invalid arguments to ctime_s." );
      return 1;
    }
    printf( "Time modified : %s", timebuf );
  }
}
Run Code Online (Sandbox Code Playgroud)

Windows 7输出:

File size     : 6
Drive         : D:
Time modified : Tue Sep  8 10:06:57 2015
Run Code Online (Sandbox Code Playgroud)

Windows XP输出:

Problem getting information: Invalid argument
Invalid parameter to _stat.
Run Code Online (Sandbox Code Playgroud)

是的crt_stat.c位于可执行文件目录中,该目录也是CWD.

这是一个Bug还是我错过了什么?

Tob*_*ann 5

正如评论中指出的那样,这是运行时的错误。目前(2015-09-09)该修复程序尚未提供更新,但可能很快就会发布。一种解决方法是GetFileAttributesEx改用。