我正在嘗試對當前作業目錄、子目錄進行排序,然后顯示它們。我的程式行為開始該程序并適用于第一個子目錄,但之后沒有。一些列印是為了檢查代碼的行為。雖然在 stackoverflow 上有完整的作業版本,但我只是想更好地了解我的代碼邏輯在哪里失敗。任何幫助是極大的贊賞。這是代碼:
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
void sortArr(char array[256][256], int amt) {
char temp[1000];
for(int i=0; i<amt; i ) {
for(int j=0; j<(amt-1-i); j ) {
if(strcmp(array[j], array[j 1]) > 0) {
strcpy(temp, array[j]);
strcpy(array[j], array[j 1]);
strcpy(array[j 1], temp);
}
}
}
printf("SortList: %s\n", array[0]);
}
void build(char *s_path, const int root) {
char path[1000];
strcat(path, s_path);
int amt=0,index;
struct dirent *dp;
DIR *dir =opendir(path);
char list[256][256];
struct stat statbuf;
if(!dir)
return;
while((dp =readdir(dir)) != NULL) {
if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0) {
strcpy(list[amt], dp->d_name);
amt ;
}
}
sortArr(list, amt);
for(int i=0; i <amt;i ) {
for (index=0; index<root; index ) {
if (index%2 == 0 || index == 0)
printf("%c", ' ');
else
printf(" ");
}
printf("%c %s\n", '-', list[i]);
strcpy(path, s_path);
strcat(path, "/");
strcat(path, list[i]);
stat(path, &statbuf);
if((S_ISDIR(statbuf.st_mode)) != 0 && (S_ISREG(statbuf.st_mode)) ==0) {
printf("DIR: %s\n", path);
build(path,root 2);
}
}
closedir(dir);
}
int main() {
build(".", 0);
};
uj5u.com熱心網友回復:
根據我的評論。
關鍵問題是path當你呼叫 時你不知道里面有什么build(),所以最后連接并沒有什么可靠的。path[0] = '\0';在呼叫之前添加strcat(),或者(更簡單)使用strcpy().
只要您的目錄中的條目不超過 256 個(除.and之外..),它就可以解決您的問題。
如果您確實有更大的目錄,則會遇到問題。這些將通過使用動態記憶體分配來解決 -malloc()等,并且strdup()可能使用 。當然,你分配的東西也應該免費。
您應該檢查呼叫是否stat()成功,但它們不會經常失敗。readdir()他們可能失敗的一次是如果在回傳名稱的時間和代碼呼叫檔案的時間之間洗掉stat()了檔案。
有用于掃描目錄的POSIX工具。這些包括:
scandir()nftw()
修改后的源代碼(包括一些未注釋的更改):
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
static
void sortArr(char array[356][256], int amt)
{
char temp[1000];
for (int i = 0; i < amt; i )
{
for (int j = 0; j < (amt - 1 - i); j )
{
if (strcmp(array[j], array[j 1]) > 0)
{
strcpy(temp, array[j]);
strcpy(array[j], array[j 1]);
strcpy(array[j 1], temp);
}
}
}
printf("SortList: %s\n", array[0]);
}
static
void build(const char *s_path, int root)
{
char path[1000];
strcpy(path, s_path);
int amt = 0;
struct dirent *dp;
DIR *dir = opendir(path);
char list[356][256];
struct stat statbuf;
if (!dir)
return;
while ((dp = readdir(dir)) != NULL)
{
if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0)
{
strcpy(list[amt], dp->d_name);
amt ;
}
}
sortArr(list, amt);
for (int i = 0; i < amt; i )
{
printf("%*s", root, "");
/*for (int index = 0; index < root; index )*/
/* putchar(' ');*/
printf("%c %s\n", '-', list[i]);
strcpy(path, s_path);
strcat(path, "/");
strcat(path, list[i]);
if (stat(path, &statbuf) != 0)
{
fprintf(stderr, "failed to stat name '%s' (%d: %s)\n", path, errno, strerror(errno));
continue;
}
if ((S_ISDIR(statbuf.st_mode)) != 0 && (S_ISREG(statbuf.st_mode)) == 0)
{
printf("DIR: %s\n", path);
build(path, root 2);
}
}
closedir(dir);
}
int main(void)
{
build(".", 0);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/522675.html
標籤:C排序目录结构
下一篇:如何從陣列中洗掉最小值和最大值?
