【问题标题】:How do I fix "'struct _IO_FILE' has no member named '_file'"?如何修复“'struct _IO_FILE'没有名为'_file'的成员”?
【发布时间】:2019-06-12 19:25:35
【问题描述】:

我有一个程序版本,它曾经编译为 *.o 文件,但现在没有,并给出了编译器错误。

我尝试在 Linux 上使用 gcc 编译器编译我的代码,但编译失败。

#include <stdio.h>
int isatty();

long isatty_(lio_number)
long *lio_number;
{
        int file_desc;

        if ((*lio_number)==5)
        {
                file_desc = stdin->_file;
                return isatty(file_desc);
        }
        else
                return 0;
}

我希望命令 gcc -c isatty.c 产生 isatty.o 但它没有。相反,我收到了这条消息:

isatty.c: In function ‘isatty_’:
isatty.c:11: error: ‘struct _IO_FILE’ has no member named ‘_file’

【问题讨论】:

  • 永远不要使用FILE 结构的任何成员。相反,请使用fileno(stdin)
  • lio_number 参数很奇特。请您至少展示一个使用此功能的代码示例吗?
  • 你从哪里得到这个代码?它是用一种古老的 C 语言编写的(并且是不可移植的)。

标签: c linux compiler-errors


【解决方案1】:

切勿使用FILE 结构的任何成员。

使用fileno(stdin) 而不是stdin-&gt;_file

成员 _file 是 MinGW 特定的文件描述符名称,而 fileno 是广泛支持的 POSIX 兼容函数。

除此之外,您可能想要#include &lt;unistd.h&gt; 而不是明确定义isatty

如果由于某种原因您仅限于以这种方式编写代码,请不要期望它是可移植的。否则,这应该可以工作:

#include <stdio.h>
#include <unistd.h>

long isatty_(long *lio_number)
{
        int file_desc;

        if (*lio_number == 5)
        {
                file_desc = fileno(stdin);
                return isatty(file_desc);
        }
        else
        {
                return 0;
        }
}

这个变化是它包含了unistd.h,它为isatty提供了一个声明,它包含了函数定义中的参数类型,并且它使用fileno(stdin)而不是stdin-&gt;_file,其中前者更便携。它还改进了格式,以便其他人可以在需要时阅读您的代码。

【讨论】:

    【解决方案2】:

    使代码现代化。原来的目标似乎是一些古老的 Unix。这应该适用于更新的 POSIX 兼容系统,因为几乎每个这样的系统都应该提供 fileno() 函数。将代码更改为标准 C 也是一个好主意。

    所以使用fileno(),包含&lt;unistd.h&gt;而不是前向声明isatty(),并使用标准C函数参数声明:

    #include <stdio.h>
    #include <unistd.h>
    
    long isatty_(long *lio_number)
    {
        if (*lio_number == 5)
        {
            return isatty(fileno(stdin));
        }
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-08-09
      • 2014-12-26
      • 2023-03-10
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-14
      • 2010-09-17
      相关资源
      最近更新 更多