fstat 有效,stat 无效

fstat works, stat does not

我正在 Raspbian 上编译 Angband 变体,但以下失败:

struct stat txt_stat, raw_stat;

/* Build the filename */
path_build(buf, 1024, ANGBAND_DIR_EDIT, template_file);

/* Access stats on text file */
if (stat(buf, &txt_stat))
{
  /* Error */
  msg_format("Oh dear, something went wrong with stat()! %s", strerror(errno));
  msg_format("Could not retrieve stats on the text file at {%s}", buf);
  return (-1);
}

让我无休止的是该文件位于 ~/git/hellband/lib/edit/r_info.txt

当我用打开文件的代码(使用相同的 buf)替换上面的代码,然后在文件描述符上使用 fstat() 时,它起作用了!!

我不想打开文件(并记得关闭该文件),我只想 know/fix stat() 发生了什么。

后记 2020;事实证明,当我使用 Angband 方法打开文件时,它会自动扩展 ~ 符号,这就是它起作用的原因,并且 fstat 起作用了。

很简单。 stat(2) 无法扩展或理解 ~(波浪号)。

您可以 HOME 代替。例如,getenv("HOME") 然后在调用 stat(2).

之前加上您的文件名作为前缀