首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >打印目录: chdir不工作

打印目录: chdir不工作
EN

Stack Overflow用户
提问于 2017-05-16 22:44:09
回答 2查看 459关注 0票数 1

我想知道是否有人能告诉我我做错了什么。这段代码应该遍历所有目录和文件,并按照UNIX实用程序查找的方式打印出来。但由于某些原因,我无法让chdir更改工作目录。我试图限制我使用的文件描述符的数量。

MAIN

代码语言:javascript
复制
#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;
}

目录方法定义

代码语言:javascript
复制
#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);
    }
  }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-16 22:59:50

问题似乎是打电话给readdir(dp).

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

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

代码语言:javascript
复制
#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

评论中的一些人推荐 (file tree walk),这是一个很好的选择,而不是自己去做。

如果这不是一个学习项目,我会推荐它的使用。

不过,请注意POSIX1.2008将ftw标记为过时,因此请确保使用nftw风格。

票数 0
EN

Stack Overflow用户

发布于 2017-05-16 23:12:08

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44012779

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档