簡體   English   中英

打印目錄:chdir無法正常工作

[英]Printing directory: chdir not working

我想知道是否有人可以告訴我我做錯了什么。 這段代碼應該遍歷所有目錄和文件,並以與UNIX實用程序FIND完全相同的方式打印出來。 但由於某種原因,我無法獲得chdir來更改工作目錄。 我正在嘗試限制使用的文件描述符的數量。

主要

#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);
    }
  }
}

問題似乎是調用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 * )只是指向內存中數據的指針。 該數據與特定目錄有關,您可以同時打開多個目錄。

編輯2

評論中的一些人推薦使用nftw (文件樹步行) ,這是自己做的一個很好的選擇。

如果這不是一個學習項目,我建議使用它。

請注意,POSIX.1-2008標志着ftw已過時,因此請務必使用nftw風格。

你的目標是學會自己實現這個,還是只想要結果? 因為你應該看一下fts.h,如果你想要一些非常強大的東西來實現像find這樣的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM