gpt4 book ai didi

c - 如何测试 FILE*(指向结构的指针)(如果 == NULL)?

转载 作者:太空宇宙 更新时间:2023-11-04 00:12:29 24 4
gpt4 key购买 nike

我一直在玩 C,不管怎样,我在想文件指针(指向结构类型)如何立即测试是否为 NULL:

FILE *cfPtr;
if ( ( cfPtr = fopen( "file.dat", "w" ) ) == NULL )

我自己尝试这样做,但出现错误。

struct foo{
int x;
};

struct foo bar = {0};

if (bar == NULL)
puts("Yay\n");
else
puts("Nay");

error C2088: '==' : illegal for struct

这是 stdio.h 文件中的 FILE 减速:

struct _iobuf {
char *_ptr;
int _cnt;
char *_base;
int _flag;
int _file;
int _charbuf;
int _bufsiz;
char *_tmpfname;
};
typedef struct _iobuf FILE;

最佳答案

它应该是一个指向结构的指针:

struct foo* bar = ...

if (bar == NULL)
puts("Yay\n");
else
puts("Nay");

确实可以测试指向结构的指针是否为空,就像指向原始类型的指针一样。

关于c - 如何测试 FILE*(指向结构的指针)(如果 == NULL)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2957549/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com