void listDir(char *dirpath){DIR *dir = opendir((const char*)dirpath);if(dir == NULL){printf("open the log directory <%s> failure\n",dirpath);return ;}struct dirent *dirp = NULL;while((dirp = readdir(dir)) != NULL){if(dirp->d_type & DT_DIR){printf("dirpath = %s\n",dirpath);if(dirp->d_name == NULL || dirp->d_name[0] == '.' || strcmp(dirp->d_name,"..") == 0)continue;sprintf(m_chTempPath,"%s%s/",dirpath,dirp->d_name);listDir(m_chTempPath);}else if(dirp->d_type & DT_REG){sprintf(m_chTemp,"%s%s",m_chTempPath,dirp->d_name);printf("m_chTemp = %s\n",m_chTemp);} elsecontinue;}closedir(dir);}代码如上,目录结构如下:/root/shanghai/jingan/xiaoming.txt /root/nanjing/xuanwu/tom.txt/root/shanghai/changning/xiaohong.txt /root/nanjing/hexi/jack.txt/root/shanghai/putuoxiaoli.txt /root/nanjing/baixia/may.txt当我执行程序时,读目录结构会出错,比如会读成 /root/shanghai/jingan/changning,而jingan和changning是并列的两个目录,这种错误该怎么改,急等大大们在线帮解决
2 回答
拉丁的传说
TA贡献1789条经验 获得超8个赞
翻阅古今
TA贡献1780条经验 获得超5个赞
12345 | else if (dirp->d_type & DT_REG) { sprintf (m_chTemp, "%s%s" ,dirpath,dirp->d_name); //---- printf ( "m_chTemp = %s\n" ,m_chTemp); } |
123456789101112131415161718192021222324252627282930 | 以下代码,我测试正常的!你试试先,如有错,请上图 void listDir( char *dirpath) { DIR *dir = opendir(( const char *)dirpath); if (dir == NULL) { printf ( "open the log directory <%s> failure\n" ,dirpath); return ; } struct dirent *dirp = NULL; printf ( "dirpath = %s\n" ,dirpath); while ((dirp = readdir(dir)) != NULL) { if (dirp->d_type & DT_DIR) { if (dirp->d_name == NULL || dirp->d_name[0] == '.' || strcmp (dirp->d_name, ".." ) == 0) continue ; sprintf (m_chTempPath, "%s%s/" ,dirpath,dirp->d_name); listDir(m_chTempPath); } else if (dirp->d_type & DT_REG) { sprintf (m_chTemp, "%s%s" ,dirpath,dirp->d_name); printf ( "m_chTemp = %s\n" ,m_chTemp); } else continue ; } closedir(dir); } |
添加回答
举报
0/150
提交
取消