【问题标题】:Visual C++ 2015 express: _stat not working on Windows XPVisual C++ 2015 express:_stat 在 Windows XP 上不起作用
【发布时间】:2015-12-03 20:12:51
【问题描述】:

使用v140_xp 作为Platform Toolset(目标 Win32)运行以下使用 Visual C++ 2015 Express 编译的 example for _stat from MSDN(目标 Win32)在 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。​​p>

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

【问题讨论】:

  • 你能用绝对路径测试你的例子吗?
  • MSDN: connect.microsoft.com/VisualStudio/feedback/details/1557168/… - 听起来像是 VS2015 的错误
  • 也许_fstat() 会起作用(我怀疑)
  • vc140_xp 似乎存在一些问题。可悲的是,我不认为他们会推动解决这个问题作为高优先级,因为 XP 已经过时了。如果出于可移植性原因,GetFileAttributesEx 不是一个选项,您可能需要考虑针对旧版本而不是等待。
  • 我们已经修复了这个错误,并正在努力将修复纳入更新。当我们锁定详细信息后,我们将使用更多信息解决 Connect 错误。谢谢。

标签: c++ visual-studio winapi visual-c++ visual-studio-2015


【解决方案1】:

正如 cmets 中所指出的,它是运行时中的 bug。目前 (2015-09-09) 修复尚未在更新中提供,但可能很快就会出现。一种解决方法是改用GetFileAttributesEx

【讨论】:

【解决方案2】:

该错误已在 Visual C++ Redistributable for Visual Studio 2015 Update 1 上得到解决

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

【讨论】:

  • 即使 update1,使用静态链接 c++ 运行时库(多线程调试),stat 仍然总是失败,参数无效
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-23
  • 2012-03-26
  • 1970-01-01
  • 2012-04-14
  • 1970-01-01
  • 2019-05-03
  • 1970-01-01
相关资源
最近更新 更多