首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

linux下遍历目录文件并生成本地页面(求解),该怎么解决

2012-03-28 
linux下遍历目录文件并生成本地页面(求解)我有一个项目,要求 用网页的形式 显示 系统中 某个目录 中的内容

linux下遍历目录文件并生成本地页面(求解)
我有一个项目,要求 用网页的形式 显示 系统中 某个目录 中的内容 ,求教思路,有代码最好
网页是用js css加载的 c++该怎么写 这个项目类似于 浏览ftp服务器网页

[解决办法]
遍历目录:

C/C++ code
//linux下://A Demo written by camelrain /*the program find  a  file  from  current  directory  or  your defined  directorycommond  optinon [path]  filename*/#include <sys/types.h>#include <sys/stat.h>#include <dirent.h>#include <pwd.h>#include <unistd.h>#include <stdio.h>#include <string.h>#define LENGTH  256/* just if is a directory*/static  int   IsDir (char * name);/* search target file, arg1 is current path, arg2 is target  file*/static  void  search_file (char * path, char * name);static  int   search_flag=0;/*just  if is a  directory*/static  int  IsDir (char * name) {   struct  stat  buff;      if (lstat(name,&buff)<0)      return 0; //if not exist name ,ignore   /*if is directory return 1 ,else return 0*/    return S_ISDIR(buff.st_mode);}/*search  target file*/static  void search_file (char * path, char * name) {   DIR         *directory;   struct      dirent * dir_entry;   char        buffer[LENGTH];   if ((directory=opendir(path)) == NULL) {       fprintf(stderr, "%s", path);       perror(" ");       return;   }      while (dir_entry=readdir(directory)) {     if  (!strcmp(dir_entry->d_name,".")||!strcmp(dir_entry->d_name,"..")) {              /*  do    nothing*/     }     else {           /* if is  boot  directory  add  "/" */           if ((strcmp(path,"/"))==0)              sprintf(buffer,"%s%s",path,dir_entry->d_name);           /*  if is not  boot  directory do not add "/"*/           else              sprintf(buffer,"%s/%s",path,dir_entry->d_name);           //printf("Now  file : %s\n",buffer);           if (IsDir(buffer))               search_file (buffer , name);           else {               if (strcmp(dir_entry->d_name,name)==0)                 {                  printf("%s\n",buffer);                  search_flag=1;                 }            }      }    }  closedir(directory);}int main(int argc, char *argv[]){   static  char * current_dir;   static  char * filename;    int     length;   if (argc==1) {         printf("A few  parameters!!\n");         return 0;      }   if (argc==2) {        current_dir=(char * )getcwd(current_dir,LENGTH);        filename=argv[1];      }   if (argc==3) {        length=strlen(argv[1]);        if (length>1 && (argv[1][length-1]=='/')) {               argv[1][length-1]='\0';               //printf("%d\n",strlen(argv[1]));           }                   current_dir=argv[1];        filename=argv[2];      }    search_file(current_dir,filename);   if (!search_flag)      printf("Not found this(%s)  file!\n",filename); return 0;} 

热点排行