免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3692 | 回复: 15

[C] 菜鸟刚入门写的查找文件或者文件夹并加密的程序,希望大牛们多多指教,能帮我优化程序 [复制链接]

论坛徽章:
0
发表于 2012-08-23 08:42 |显示全部楼层
本帖最后由 mkshtk 于 2012-08-23 21:20 编辑

程序需要实现功能:输入name和path,查找并判断name是文件还是文件夹,若name是文件则选择加密,若是目录则遍历该目录,对下面的文件进行加密。
      加密算法暂时没有写,只是大概要实现功能。希望大牛们多多指点,帮我删减优化掉一些字段,减小耦合度,能实现模块化。谢谢
  1. # include <dirent.h>
  2. # include <string.h>
  3. # include <stdio.h>
  4. # include <sys/stat.h>
  5. # include <stdlib.h>
  6. # include <unistd.h>
  7. # include <stdbool.h>

  8. /*
  9. 函数名:jiami()
  10. 作用:解密算法
  11. 参数:* stream--需要加密的字符串指针
  12.           * store1--加密完成后的字符串指针
  13. */
  14. int jiami(char *stream1, char *store1)
  15. {       
  16.         int i, len;
  17.         len = strlen(stream1);
  18.         for(i=0; i<len; i++)
  19.         {       
  20.                 store1[i] = stream1[i];
  21.         }
  22.         return 0;
  23. }

  24. /*
  25. 函数名:jiemi()
  26. 作用:解密算法
  27. 参数:* stream--需要解密的字符串指针
  28.           * store--解密完成后的字符串指针
  29. */
  30. int jiemi(char *stream2, char *store2)
  31. {       
  32.         int i, len;
  33.         len = strlen(stream2);
  34.         for(i=0; i<len; i++)
  35.         {       
  36.                 store2[i] = stream2[i]+1;
  37.         }
  38.         return 0;
  39. }

  40. /*
  41. 函数名:En_Code()
  42. 作用:加密函数
  43. 参数:* pSrc--需要加密的字符串指针
  44.           * pDst--生成目标文件的文件指针
  45. */
  46. void En_Code(char *pSrc, FILE *pDst)
  47. {       
  48.         char *out1 = (char *)malloc(sizeof(pSrc)*100);
  49.         jiami(pSrc,out1);
  50.         fwrite(out1, strlen(out1), 1, pDst);
  51.         free(out1);
  52.         out1 = NULL;
  53. }


  54. /*
  55. 函数名:De_Code()
  56. 作用:解密函数
  57. 参数:* pSrc--需要解密的字符串指针
  58.           * pDst--生成目标文件的文件指针
  59. */
  60. void De_Code(char *pSrc, FILE *pDst)
  61. {       
  62.         char *out2 = malloc(sizeof(pSrc)*100);
  63.         jiemi(pSrc,out2);
  64.         fwrite(out2, strlen(out2), 1, pDst);
  65.         free(out2);
  66.         out2 = NULL;
  67. }


  68. /*
  69. 函数名:Open_file()
  70. 作用:判断源文件和需要生成的目标文件是否能够打开
  71. 参数:*Src_file--源文件指针
  72.           *Dst_file--生成目标文件指针
  73. */
  74. int  Open_file(char *Src_file, char *Dst_file)
  75. {
  76.         FILE *fp1, *fp2;
  77.         long curpos, flen;
  78.         char *buff;
  79.         fp1=fopen(Src_file, "rb");
  80.         fp2=fopen(Dst_file, "wb+");
  81.         if(fp1 == NULL)
  82.         {
  83.                 printf("cannot open source file.\n");
  84.                 exit(1);
  85.         }
  86.                 if(fp2 == NULL)
  87.         {
  88.                 printf("cannot open destination file.\n");
  89.                 exit(1);
  90.         }
  91.        
  92.         curpos = ftell(fp1);
  93.         fseek(fp1, 0L, SEEK_END);
  94.         flen = ftell(fp1);
  95.         buff = (char *)malloc(flen*10);
  96.         memset(buff, 0, flen*10);
  97.         if(buff == NULL)
  98.         {
  99.                 fclose(fp1);
  100.                 return 0;
  101.         }
  102.         fseek(fp1, curpos, SEEK_SET);
  103.         fread(buff, flen, 1, fp1);
  104.         En_Code(buff,fp2);
  105.         free(buff);
  106.         buff = NULL;
  107.         fclose(fp1);
  108.         fclose(fp2);
  109.         return 0;
  110. }

  111. /*
  112. 函数名:File_Exist()
  113. 作用:判断指定路径下文件是否存在,不查询含子目录
  114. 参数:* fullpath---完整的路径加上文件名
  115. */
  116. bool File_Exist(char * fullpath)
  117. {
  118.         if((access(fullpath,F_OK)) == 0)
  119.                 return true;
  120.         else
  121.                 return false;
  122. }

  123. /*
  124. 函数名:List()
  125. 作用:循环便利目录,并对查找的文件进行加密等操作
  126. 参数:* path--要查找的目录地址指针
  127. */
  128. void List(char * path)
  129. {
  130.         struct dirent *ent = NULL;
  131.         DIR *pDir;
  132.         char dir[512];
  133.         struct stat statbuf;
  134.         char * store_path = "store";
  135.         char store_fullpath[512];

  136.         if((pDir = opendir(path)) == NULL)
  137.         {
  138.                 fprintf(stderr, "cannot open directory:%s\n", path);
  139.                 return;
  140.         }

  141.         while((ent = readdir(pDir)) != NULL)
  142.         {
  143.                 snprintf(dir, 512, "%s/%s", path, ent->d_name);
  144.                 lstat(dir, &statbuf);

  145.                 if(S_ISDIR(statbuf.st_mode))
  146.                 {
  147.                         if(strcmp(".", ent->d_name) == 0 || strcmp("..", ent->d_name) == 0)
  148.                                 continue;
  149.                         List(dir);
  150.                 }
  151.                
  152.                 if(S_ISREG(statbuf.st_mode))
  153.                 {
  154.                         snprintf(store_fullpath, 512, "%s/%s", store_path, ent->d_name);
  155.                         Open_file(dir, store_fullpath);
  156.                 }
  157.         }
  158.         closedir(pDir);
  159. }

  160. /*
  161. 函数名:Search_Dir()
  162. 作用:遍历查找指定目录下文件是否存在
  163. 参数:* path---查找的路径
  164.           * name--文件名
  165. */
  166. bool Search_Dir(char * path, char *name)
  167. {       
  168.         char dir[50];
  169.         struct dirent * ent;
  170.         DIR *pdir;
  171.         struct stat statbuf;

  172.         if((pdir = opendir(path)) == NULL)
  173.         {
  174.                 fprintf(stderr, "Cannot open directory:%s\n", path);
  175.                 return false;
  176.         }

  177.         while((ent = readdir(pdir))!=NULL)
  178.         {
  179.                 if((strncmp(ent->d_name,".",1))==0 || (strncmp(ent->d_name,"..",2))==0)
  180.                         continue;
  181.                 snprintf(dir,50,"%s/%s", path, ent->d_name);
  182.                 lstat(dir, &statbuf);
  183.                
  184.                 if(strncmp(name,ent->d_name,sizeof(name))==0)
  185.                 {
  186.                         if(S_ISDIR(statbuf.st_mode))
  187.                         {
  188.                                 printf("2:'%s' is dir and exist!-----path:'%s'\n",name,dir);
  189.                                 List(dir);
  190.                         }
  191.                         if(S_ISREG(statbuf.st_mode))
  192.                         {       
  193.                                 printf("'%s' id file and exist!-----full path:'%s'\n",name,dir);       
  194.                         }
  195.                         return true;
  196.                 }
  197.                 else
  198.                 {                       
  199.                         if(S_ISDIR(statbuf.st_mode))
  200.                         {
  201.                                 Search_Dir(dir,name);
  202.                         }
  203.                 }
  204.         }
  205.         closedir(pdir);
  206.         return 0;
  207. }

  208. int main()
  209. {       
  210.         struct stat main_buff;
  211.         char name[50];
  212.         char path[50];
  213.         char path_name[50];
  214.         char output[50];
  215.         printf("Please input the path:\n");
  216.         scanf("%s", path);

  217.         printf("Please input the file name:\n");
  218.         scanf("%s", name);

  219.         printf("Please input the output path and file name:\n");
  220.         scanf("%s", output);

  221.         snprintf(path_name, 50, "%s/%s", path, name);

  222.         if(File_Exist(path_name))
  223.         {
  224.                 printf("1:The file '%s' exist!---path:'%s'\n", name, path_name);
  225.                 lstat(path_name, &main_buff);
  226.                 if(S_ISDIR(main_buf.st_mode))
  227.                 {
  228.                         if(strcmp(".", ent->d_name) == 0 || strcmp("..", ent->d_name) == 0)
  229.                                 continue;
  230.                         List(dir);
  231.                 }
  232.                
  233.                 if(S_ISREG(main_buff.st_mode))
  234.                 {
  235.                         Open_file(path_name,output);
  236.                 }
  237.         }
  238.         else
  239.                 Search_Dir(path, name);
  240.         return 0;
  241. }
复制代码

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
发表于 2012-08-23 08:54 |显示全部楼层
看到第一个函数
len = sizeof(stream1); 就挺扯的

论坛徽章:
0
发表于 2012-08-23 09:41 |显示全部楼层
回复 2# bruceteen


    不好意思,应该是strlen,忘记改了

论坛徽章:
0
发表于 2012-08-23 10:50 |显示全部楼层
int jiami(char *stream1, char *store1)
{        
        int i, len;
        len = strlen(stream1);
        for(i=0; i<len; i++)
        {        
                store1 = stream1;                 <- 啥意思?干吗放在循环里?
        }
        return 0;
}

论坛徽章:
0
发表于 2012-08-23 14:40 |显示全部楼层
回复 4# foxwb


    准备用来加密的,现在还没写加密函数哦

论坛徽章:
0
发表于 2012-08-23 15:59 |显示全部楼层
进来学习下

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
发表于 2012-08-23 17:26 |显示全部楼层
建议将代码直接用 code代码 框起来。楼主的代码好像掉了一些运算符。

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
发表于 2012-08-23 17:32 |显示全部楼层
建议改成命令行参数输入。这样适用性好一些。还有就是只有加密流程,没有解密流程。可以做成自动模式(在加密文件中增加唯一标识)。或者通过参数手动指定。
至于加密和解密算法,可以留到最后来实现。还有就是提示:加密和解密算法一般都需要密匙。无密匙的算法很容易破解。

论坛徽章:
0
发表于 2012-08-23 19:43 |显示全部楼层
回复 8# cobras


    谢谢大牛指点啊,我目前还只是想简单的完成这个流程,解密的话我打算加入判断让用户选择加密或者解密。至于算法还没研究过呢,等整个框架都完成了,在慢慢的局部改进。目前在文件夹加密这块卡住了,而且感觉越写耦合度越高。。等彻底完成了再贴出来,希望大牛们能给我这个菜鸟指点下,谢谢啊

论坛徽章:
0
发表于 2012-08-23 19:46 |显示全部楼层
回复 7# cobras


    额。第一次发代码,还不知道怎么怎么贴代码...
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP