- 论坛徽章:
- 0
|
在sco_unix5.0.5下:
如何遍历目录下的所有文件。
#include <sys/types.h>;
#include <sys/stat.h>;
#include <sys/dir.h>;
#include <sys/dirent.h>;
#include <string.h>;
#include <stdio.h>;
#include <dirent.h>;
main()
{
struct direct *dp;
DIR *dirp;
struct stat sbuf;
int i;
dp =(struct direct *)malloc(sizeof(struct direct ));
dirp=(DIR *)malloc(sizeof(DIR));
memset(tmp,0,255);
dirp = opendir( "." );
i=1;
while((dp = readdir( dirp )) != NULL ) {
if(dp->;d_ino==0) continue;
strcpy(tmp,dp->;d_name);
printf("[%02d]dp->;d_name=%s,len=%d,d_reclen=%d\n",i,dp->;d_name,strlen(dp->;d_name),dp->;d_reclen);
i++;
}
closedir( dirp );
free(dp);
free(dirp);
}
==========================
运行结果:
[01]dp->;d_name=,len=0,d_reclen=12
[02]dp->;d_name=,len=0,d_reclen=16
[03]dp->;d_name=,len=1,d_reclen=24
[04]dp->;d_name=,len=1,d_reclen=20
[05]dp->;d_name=,len=1,d_reclen=20
[06]dp->;d_name=,len=1,d_reclen=12
[07]dp->;d_name=,len=1,d_reclen=20
[08]dp->;d_name=,len=0,d_reclen=16
[09]dp->;d_name=,len=1,d_reclen=16
++++++++++++++++++++++
why?
dp->;d_name 的内容为空!
|
|