您的位置:首页 > 运维架构 > Linux

linux c/c++ 读取指定目录下的文件名

2016-11-26 20:17 197 查看
#include <dirent.h>
#include <stdio.h>
/*struct dirent
{
long d_ino; // inode number 索引节点号
off_t d_off; // offset to this dirent 在目录文件中的偏移
unsigned short d_reclen; // length of this d_name 文件名长
unsigned char d_type; // the type of d_name 文件类型
char d_name [NAME_MAX+1]; // file name (null-terminated) 文件名,最长255字符
}
其中d_type表明该文件的类型:文件(8)、目录(4)、链接文件(10)等
*/
int main(){
DIR *directory_pointer;
struct dirent *entry;
if((directory_pointer=opendir("/home/libin/桌面"))==NULL){
printf("Error open\n");
return ;
} else {
while((entry=readdir(directory_pointer))!=NULL){
if(entry->d_name[0]=='.') continue;
printf("%s\n",entry->d_name);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: