Visual C ++ 2015 Express:_stat在Windows XP上不起作用 [英] Visual C++ 2015 express: _stat not working on Windows XP

查看:236
本文介绍了Visual C ++ 2015 Express:_stat在Windows XP上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行以下使用Visual C ++编译的来自MSDN的_stat的示例v140_xp用作Platform Toolset(目标Win32)的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 XP输出:

Problem getting information: Invalid argument
Invalid parameter to _stat.

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

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

解决方案

正如注释中指出的,它是example for _stat from MSDN compiled with Visual C++ 2015 Express using v140_xp as Platform Toolset (target Win32) runs normally on Windows 7 but not on Windows XP on several machines I tested.

// 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 output:

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

Windows XP output:

Problem getting information: Invalid argument
Invalid parameter to _stat.

And yes crt_stat.c is located in the executables directory which also is the CWD.

Is this a Bug or am I missing something?

解决方案

As pointed out in the comments, it is a bug in the runtime. Right now (2015-09-09) the fix is not yet available in an update, but probably will be soon. A workaround is to use GetFileAttributesEx instead.

这篇关于Visual C ++ 2015 Express:_stat在Windows XP上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆