[英]c programming - directory name from DIR* struct
从dirent.h中,我们可以看到DIR结构是
struct DIR {
struct dirent ent;
struct _WDIR *wdirp;
};
而Dirent结构是
struct dirent {
/* Always zero */
long d_ino;
/* File position within stream */
long d_off;
/* Structure size */
unsigned short d_reclen;
/* Length of name without \0 */
size_t d_namlen;
/* File type */
int d_type;
/* File name */
char d_name[PATH_MAX+1];
};
我的问题是:如果只有DIR * struct -let说dir- ,并且要从此struct中提取目录名,通常-应该吗? -做:
const char * dirname = dir->ent.d_name;
但是这不能编译,错误是:
dereferencing pointer to incomplete type
谢谢
您不应该直接访问DIR
的内容。
要遍历目录中的条目,您需要调用readdir
,并向其传递从opendir
返回的DIR *
。 这将返回dirent *
,您可以从中读取目录条目的名称。
读取所有条目后,该函数将返回NULL
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.