Visual C++ 2015 Express:_stat 无法在 Windows XP 上运行

Visual C++ 2015 express: _stat not working on Windows XP

运行 以下 example for _stat from MSDN 使用 v140_xp 作为 Platform Toolset(目标 Win32)使用 Visual C++ 2015 Express 编译,但在 Windows 7 上正常运行在 Windows 我测试过的几台机器上的 XP 上。

// 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 );
  }
}

Windows 7 输出:

File size     : 6
Drive         : D:
Time modified : Tue Sep  8 10:06:57 2015

Windows 经验输出:

Problem getting information: Invalid argument
Invalid parameter to _stat.

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

这是一个错误还是我遗漏了什么?

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

此错误已在 Visual C++ Redistributable for Visual Studio 2015 Update 1

上解决

我通过在此处安装该更新表单解决了问题:https://www.microsoft.com/en-us/download/details.aspx?id=49984