打印目录:chdir无法正常工作

时间:2017-05-16 22:44:09

标签: c

我想知道是否有人可以告诉我我做错了什么。这段代码应该遍历所有目录和文件,并以与UNIX实用程序FIND完全相同的方式打印出来。但由于某种原因,我无法获得chdir来更改工作目录。我试图限制使用的文件描述符的数量。

MAIN

#include <stdio.h>
#include "sfind.h"
#include <unistd.h>
#include <dirent.h>
int main(int argv, char *argc[]){
    char cwd[1024]; /* current working directory limit*/
    char *path = NULL;
    DIR *dp = NULL;
    if (getcwd(cwd, sizeof(cwd)) != NULL){ /*allow us to grab the current working directory*/
      fprintf(stdout, "Current working dir: %s\n", cwd);
    }
    else{
        perror("getcwd() error");
    }
    dp = opendir(cwd);
    path = ".";
    directoryList(dp,path);
    return 0;
}

目录方法定义

#include <stdio.h>
#include <stdlib.h>
#include "sfind.h"
#include <unistd.h>
#include <limits.h>
#include <dirent.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

void directoryList(DIR *dp, char *path){
    char newPath[PATH_MAX] = {0};/*To store new path*/
  struct dirent *element; /*get file name*/
  struct stat statbuf;/*determine type of file*/
  int status = 0; /*My base case should be once the directory I'm in runs outs out of files I should return;*/
  if(dp == NULL){
    fprintf(stderr,"FILE DID NOT OPEN!");
    exit(-1);
  }
  /*change the current file directory even if its the first one*/  
  if((status = chdir(path)) == -1){
    printf("ERROOR!");
  }/*change the current working directory whether that the same on or not*/

  while((element = readdir(dp)) != NULL) /*while current file directory pointer is not equal to zero*/{ 
    /* from here we only have two cases once were reading from the directory either is a file or directory!*/
    /*using lstat*/

    lstat(element->d_name,&statbuf);

    if((S_ISDIR(statbuf.st_mode))) /*is of type directory*/{
        if((strcmp(".",element->d_name) == 0) || (strcmp("..",element->d_name) == 0))
        continue;
      /*create new directory name*/
      newPath[0] = '\0';
      strcat(newPath,path);/* this will give us the "."*/
      strcat(newPath,"/");
      strcat(newPath,element->d_name);
      printf("%s\n", newPath);
      directoryList(dp,newPath); /*recursion*/ 
      file*/
    }
    else /*Its a file!*/{
        printf("%s/%s\n",path,element->d_name);
    }
  }
}

2 个答案:

答案 0 :(得分:0)

问题似乎是调用readdir(dp) ...

即使您更改了当前的工作目录,也不会更新dp指针以打开新文件夹。

这是一个穷人的工作示例(我不会这样做,但它适用于小树)。

#include <dirent.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

void directoryList(DIR *dp, char *path) {
  char newPath[PATH_MAX] = {0}; /*To store new path*/
  struct dirent *element;       /*get file name*/
  struct stat statbuf;          /*determine type of file*/
  int status = 0;               /*My base case should be once the directory I'm in runs outs
                                   out of files I should return;*/
  DIR *dp_tmp;
  if (dp == NULL) {
    fprintf(stderr, "FILE DID NOT OPEN!");
    exit(-1);
  }

  while ((element = readdir(dp)) !=
         NULL) /*while current file directory pointer is not equal to zero*/ {
    /* from here we only have two cases once were reading from the directory
     * either is a file or directory!*/
    /*using lstat*/
    lstat(element->d_name, &statbuf);

    if ((S_ISDIR(statbuf.st_mode))) /*is of type directory*/ {
      if ((strcmp(".", element->d_name) == 0) ||
          (strcmp("..", element->d_name) == 0))
        continue;
      /*create new directory name*/
      newPath[0] = '\0';
      strcat(newPath, path); /* this will give us the "."*/
      strcat(newPath, "/");
      strcat(newPath, element->d_name);
      printf("%s\n", newPath);
      if ((dp_tmp = opendir(newPath)) == NULL) {
        perror("hmm?! ");
        exit(1);
      }
      directoryList(dp_tmp, newPath); /*recursion*/
    } else /*Its a file!*/ {
      printf("* %s/%s\n", path, element->d_name);
    }
  }
  closedir(dp);
}

int main(void) {
  char cwd[1024]; /* current working directory limit*/
  char *path = NULL;
  DIR *dp = NULL;
  if (getcwd(cwd, sizeof(cwd)) !=
      NULL) { /*allow us to grab the current working directory*/
    fprintf(stdout, "Current working dir: %s\n", cwd);
  } else {
    perror("getcwd() error");
  }
  dp = opendir(cwd);
  path = ".";
  directoryList(dp, path);
  return 0;
}

修改

回答评论中的问题......

打开目录(应该使用closedir关闭)与当前工作目录不同(并且完全不相关)。

当前工作目录主要用于解析您引用的任何文件/文件夹的路径。

打开目录指针(DIR *)只是指向内存中数据的指针。该数据与特定目录有关,您可以同时打开多个目录。

<强> EDIT2

评论中的一些人建议nftw (file tree walk)这是自己做的一个很好的选择。

如果这不是一个学习项目,我建议使用它。

但是,请注意POSIX.1-2008标记为ftw已废弃,因此请务必使用nftw风格。

答案 1 :(得分:0)

你的目标是学会自己实现这个,还是只想要结果?因为如果你想要一些非常强大的东西来实现像find这样的东西,你应该看看fts.h。