stat函数的使用
stat(const char *restrict path, struct stat *restrict buf)
说明:其中path
变量不能只是当前目录下的文件名,那样会出现未找到文件或者目录的错误,应该是文件的绝对路径,
并将转化后的信息放到buf
中。目前网上很多c实现都是写的读到的文件名。这里说明一下。
其中linux下man手册里的也是用的文件名 (dp->d_name)
count_tiff_file = 0;
DIR *pDir;
struct dirent *pEntry;
struct stat buf;
pDir = opendir(PATH);
if(pDir == NULL)
{
qDebug() << "opendir error";
return;
}
while((pEntry = readdir(pDir)) != NULL)
{
if(strcmp(pEntry->d_name, ".") == 0 || strcmp(pEntry->d_name, "..") == 0)
continue; //jump not show file
//这个时候不能直接用pEntry->d_name 作为stat的参数,这只是文件名,参数应该为绝对路径
//stat(pEntry->d_name,&buf);
}
}
closedir(pDir);
获取指定目录下的文件信息,类似于实现ls