您现在的位置: 中国男护士网 >> 考试频道 >> 计算机等级 >> 二级辅导 >> C语言 >> 辅导 >> 正文    
  怎么删除一个非空的目录? 【注册男护士专用博客】          

怎么删除一个非空的目录?

www.nanhushi.com     佚名   不详 

  这是还没写好的源码:

  #include <stdio.h>
  #include <stdlib.h>
  #include <dirent.h>
  #include <unistd.h>
  #include <sys/types.h>
  int main( void )
  {
      DIR* dirp;
      struct dirent* direntp;
      struct stat    buf;
      pid_t          pid;
      dirp = opendir( "/share/tmp" );/*打开一个目录 */
      if( dirp == NULL ) {
               perror( "can't open /share/tmp" );
      } else {
          for( ; ; ) {
                direntp = readdir( dirp ); /*读目录里的内容*/
                if( direntp == NULL ) /*如果是个空目录就把它删除*/
                      rmdir(/share/tmp);
              for( ; ; ) {
                      if(lstat(directp->s_name,&buf)<0) { /*读出文件名*/
                             printf("lstat error\n");
                             exit(-1);
                      }else{
                             if( ! S_ISDIR(buf.st_mode)) < 0) { /*判断是否是一个目录*/
                                     printf("check file mode error\n");
                                     exit(1);
                             }else if
                                     unlink(directp->s_name) ; /*如果不是就删除它*/
                             else
                                     if((pid=fork())<0) {/*如果是就判断是否是一个空目录,是就删除它,这里考虑得不周到,fork一个进程,运行上面的算法,就是这里不懂了。要写个函数吧?*/
                                               printf("fork error\n");
                                               exit(1);
                                     else if(pie == 0 )
                                               chdir(directp->s_name);/*新的进程cd到新的目录*/
          }
          closedir( dirp );
      }
      return EXIT_SUCCESS;
  }

 

文章录入:杜斌    责任编辑:杜斌 
  • 上一篇文章:

  • 下一篇文章:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
     

    联 系 信 息
    QQ:88236621
    电话:15853773350
    E-Mail:malenurse@163.com
    免费发布招聘信息
    做中国最专业男护士门户网站
    最 新 热 门
    最 新 推 荐
    相 关 文 章
    没有相关文章
    专 题 栏 目