目前我有一些代码(精简和删除一堆错误检查):
dp = readdir(dir); if (dp->d_type == DT_DIR) { }
这在我的Linux机器上游泳.但是在另一台机器上(看起来像SunOS,sparc):
SunOS HOST 5.10 Generic_127127-11 sun4u sparc SUNW,Ultra-5_10
在编译时我收到以下错误:
error: structure has no member named `d_type' error: `DT_DIR' undeclared (first use in this function)
我以为dirent.h头是crossplatform(对于POSIX机器).有什么建议么.
参考
http://www.nexenta.org/os/Porting_Codefixes:
The struct dirent definition in solaris does not contain the
d_type
field. You would need to make the changes as follows
if (de->d_type == DT_DIR) { return 0; }
changes to
struct stat s; /*include sys/stat.h if necessary */ .. .. stat(de->d_name,&s); if (s.st_mode & S_IFDIR) { return 0; }
由于stat也是POSIX标准,应该是跨平台的.但是您可能想要使用if((s.st_mode& S_IFMT)== S_IFDIR)遵循标准.