Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1251564
  • 博文数量: 548
  • 博客积分: 7597
  • 博客等级: 少将
  • 技术积分: 4224
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-15 13:21
个人简介

嵌入式软件工程师&&太极拳

文章分类

全部博文(548)

文章存档

2014年(10)

2013年(76)

2012年(175)

2011年(287)

分类: LINUX

2011-03-05 21:38:06

 

  1. [linux程序]

  2. #ifndef _FILELIST_H_
  3. #define _FILELIST_H_

  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>

  7. #include <sys/types.h>
  8. #include <dirent.h>
  9. #include <sys/stat.h>
  10. #include <unistd.h>

  11. #include <errno.h>

  12. #define ROOTDIR "/root/mm"

  13. void gen(FILE * fout, const char * absolutePath, const char * virtualpath, int * item, int * totalcount);
  14. int gen_filelist(char ** inibuf, const char * rootdir);

  15. #endif /* _FILELIST_H_ */
  16. --------------------------------------------------------------------------------------------------------------------------

  17. #include "filelist.h"

  18. void gen(FILE * fout, const char * absolutePath, const char * virtualpath, int * item, int * totalcount)
  19. {
  20. //fout, rootdir, "/", &item, &totalcount


  21. /* item19_parent = "/tmp/mp3/" */
  22. /* item19_name = "1.mp3" */
  23. /* item19_isdir = 1 */

  24.  DIR *dir;
  25.  struct dirent *s_dir;
  26.  struct stat filestat;

  27.  char * filename;
  28.  char * virtualpathname;

  29.  dir = opendir(absolutePath);
  30.  if (dir)
  31.  {
  32.   while ((s_dir = readdir(dir)) != NULL)
  33.   {
  34.    if ((strcmp(s_dir->d_name, ".") == 0) || (strcmp(s_dir->d_name, "..") == 0))
  35.     continue;
  36.    
  37.    printf("1.----------------------------------------------------------------\n");
  38.    printf("absolutePath =%s, s_dir_name = %s\n", absolutePath, s_dir->d_name);
  39.    asprintf(&filename, "%s/%s", absolutePath, s_dir->d_name);
  40.    stat(filename, &filestat);

  41.    printf("2.----------------------------------------------------------------\n");
  42.    if(S_ISREG(filestat.st_mode))
  43.    {
  44.     printf("item%d_parent=\"%s\"\r\nitem%d_name=\"%s\"\r\nitem%d_isdir=%d\r\n",
  45.       *item, virtualpath,
  46.       *item, s_dir->d_name,
  47.       *item, 0
  48.            );
  49.    
  50.     fprintf(fout, "item%d_parent=\"%s\"\r\nitem%d_name=\"%s\"\r\nitem%d_isdir=%d\r\n",
  51.       *item, virtualpath,
  52.       *item, s_dir->d_name,
  53.       *item, 0
  54.            );
  55.    
  56.     ++(*item);
  57.     (*totalcount) += 3;
  58.     printf("*totalcount = %d\n", *totalcount);
  59.    }
  60.    else if(S_ISDIR(filestat.st_mode))
  61.    {
  62.     printf("===========================================================\n");
  63.     printf(" item%d_parent=\"%s\"\r\nitem%d_name=\"%s\"\r\nitem%d_isdir=%d\r _test\n",
  64.                                                 *item, virtualpath,
  65.                                                 *item, s_dir->d_name,
  66.                                                 *item, 1);

  67.     fprintf(fout, "item%d_parent=\"%s\"\r\nitem%d_name=\"%s\"\r\nitem%d_isdir=%d\r\n",
  68.       *item, virtualpath,
  69.       *item, s_dir->d_name,
  70.       *item, 1
  71.            );
  72.     ++(*item);
  73.     (*totalcount) += 3;
  74.     
  75.     asprintf(&virtualpathname, "%s%s/", virtualpath, s_dir->d_name);
  76.     printf("filestat.st_mode = %s%s/", virtualpath, s_dir->d_name);

  77.     printf("\n---------------------------------------------------------------\n");
  78.     
  79.     gen(fout, filename, virtualpathname, item, totalcount);
  80.     free(virtualpathname);
  81.    }

  82.    free(filename);
  83.   }

  84.   closedir(dir);
  85.  }
  86. };

  87. int gen_filelist(char ** inibuf, const char * rootdir)
  88. {
  89. //&filelist ROOTDIR

  90.  FILE * fout;
  91.  int length;
  92.  char * buffer;
  93.  char Zero[1] = { 0 };
  94.  int item = 0;
  95.  int totalcount = 0;

  96.  do
  97.  {
  98.   errno = 0;
  99.   fout = tmpfile();
  100.  } while ((fout == NULL) && (errno == EINTR));

  101.  gen(fout, rootdir, "/", &item, &totalcount);
  102.  fwrite(&Zero, 1, 1, fout);
  103.  fflush(fout);

  104.  /* get file length; */
  105.  fseek(fout, 0L, SEEK_END);
  106.  length = ftell(fout);
  107.  printf("gen_filelist file length : %d\n", length);

  108.  buffer = malloc(length);
  109.  fseek(fout, 0L, SEEK_SET);
  110.  fread(buffer, 1, length, fout);

  111.  fclose(fout);

  112.  *inibuf = buffer;
  113.  return totalcount;
  114. };

  115. int main(void)
  116. {
  117.  int count;
  118.  char * filelist;

  119.  count = gen_filelist(&filelist, ROOTDIR);

  120.  printf("filelist = %s\n", filelist);
  121.  printf("count = %d\n", count);

  122.  return 0;
  123. }




  124. ======================================================================

  125. [window程序]

  126. /* 头文件 */
  127. #include <windows.h>
  128. #include <stdio.h>
  129. #include <sys/stat.h>

  130. #define ROOTDIR "E:\\mm"

  131. #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
  132. #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)

  133. /* ************************************
  134. * DWORD EnumerateFileInDrectory(LPSTR szPath)
  135. * 功能 遍历目录下的文件和子目录,将显示文件的
  136. * 文件和文件夹隐藏、加密的属性
  137. * 参数 LPTSTR szPath,为需遍历的路径
  138. * 返回值 0代表执行完成,1代码发生错误
  139. **************************************/
  140. DWORD EnumerateFileInDrectory(FILE* fout, LPSTR absolutePath, char * virtualpath, int * item, int * totalcount)
  141. {
  142.  WIN32_FIND_DATA FindFileData;
  143.  HANDLE hListFile;
  144.  CHAR szFilePath[MAX_PATH];
  145.  char filename[1024];
  146.     char virtualpathname[1024];
  147.    
  148.   struct stat filestat;
  149.  //构造代表子目录和文件夹路径的字符串,使用通配符“*”

  150.  lstrcpy(szFilePath, absolutePath);
  151.  //注释的代码可以用于查找所有以“.txt结尾”的文件。

  152.  //lstrcat(szFilePath, "\\*.txt");

  153.  lstrcat(szFilePath, "\\*");
  154.  //查找第一个文件/目录,获得查找句柄

  155.  hListFile = FindFirstFile(szFilePath,&FindFileData);
  156.  //判断句柄

  157.  if(hListFile==INVALID_HANDLE_VALUE)
  158.  {
  159.   printf("错误:%d",GetLastError());
  160.   return 1;
  161.  }
  162.  else
  163.  {
  164.   do
  165.   {
  166.     //如果不想显示代表本级目录和上级目录的“.”和“..”,

  167.     //可以使用注释部分的代码过滤。

  168.    if(lstrcmp(FindFileData.cFileName,TEXT("."))==0||
  169.     lstrcmp(FindFileData.cFileName,TEXT(".."))==0)
  170.    {
  171.     continue;
  172.    }
  173.    
  174.    sprintf(filename, "%s\\%s", absolutePath, FindFileData.cFileName);
  175.    stat(filename, &filestat);

  176.    //!!test打印文件名、目录名

  177.    printf("%s\t\t",FindFileData.cFileName);
  178.   
  179.    if(S_ISREG(filestat.st_mode))
  180.    {
  181.       printf("%s\\%s\n", absolutePath, FindFileData.cFileName);
  182.       fprintf(fout, "item%d_parent=\"%s\"\r\nitem%d_name=\"%s\"\r\nitem%d_isdir=%d\r\n",
  183.        *item, virtualpath,
  184.        *item, FindFileData.cFileName,
  185.        *item, 0
  186.        );

  187.       ++(*item);
  188.       (*totalcount) += 3;
  189.       printf("*totalcount = %d\n", *totalcount);
  190.       
  191.    }
  192.    else if(S_ISDIR(filestat.st_mode))
  193.    {
  194.       printf("item%d_parent=\"%s\"\r\nitem%d_name=\"%s\"\r\nitem%d_isdir=%d\r\n",
  195.        *item, virtualpath,
  196.        *item, FindFileData.cFileName,
  197.        *item, 1
  198.        );

  199.       fprintf(fout, "item%d_parent=\"%s\"\r\nitem%d_name=\"%s\"\r\nitem%d_isdir=%d\r\n",
  200.        *item, virtualpath,
  201.        *item, FindFileData.cFileName,
  202.        *item, 1
  203.        );
  204.       ++(*item);
  205.       (*totalcount) += 3;

  206.       sprintf(&virtualpathname, "%s%s/", virtualpath, FindFileData.cFileName);
  207.       EnumerateFileInDrectory(fout, filename, virtualpathname, item, totalcount);
  208.    }
  209.    /*
  210.    //判断文件属性,加密文件或文件夹
  211.    if(FindFileData.dwFileAttributes&FILE_ATTRIBUTE_ENCRYPTED)
  212.    {
  213.     printf("<加密> ");
  214.    }
  215.    //判断文件属性,隐藏文件或文件夹
  216.    if(FindFileData.dwFileAttributes&FILE_ATTRIBUTE_HIDDEN)
  217.    {
  218.     printf("<隐藏> ");
  219.    }
  220.    //判断文件属性,目录
  221.    if(FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
  222.    {
  223.     printf(" ");
  224.    }
  225.    //读者可根据文件属性表中的内容自行添加判断文件属性。
  226.    */

  227.    printf("\n");
  228.   }
  229.   while(FindNextFile(hListFile, &FindFileData));
  230.  }
  231.  return 0;
  232. }

  233. /* ************************************
  234. * int main(int argc, PCHAR argv[])
  235. * 功能 调用ListFileInDrectory
  236. * 遍历目录下的文件和子目录
  237. * 参数 argv[1]为需遍历的路径,如果为空则获取
  238. * 当前路径
  239. **************************************/
  240. int main(int argc, PCHAR argv[])
  241. {
  242.  /*
  243.  if(argc == 2)
  244.  {
  245.   */
  246.  int item = 0;
  247.  int totalcount = 0;
  248.  FILE * fout;

  249.  fout = tmpfile();
  250.  EnumerateFileInDrectory(fout, ROOTDIR, "\\", &item, &totalcount);
  251.  /*
  252.  }
  253.  else
  254.  {
  255.   CHAR szCurrentPath[MAX_PATH];
  256.   GetCurrentDirectory(MAX_PATH,szCurrentPath);
  257.   EnumerateFileInDrectory(szCurrentPath);
  258.  }
  259.  */

  260.  while(1);
  261.  return 0;
  262. }
阅读(1200) | 评论(0) | 转发(0) |
0

上一篇:15.c

下一篇:winc与linux网络通讯

给主人留下些什么吧!~~