arrays - 文件未在 C 中排序
问题描述
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
// This program is going to scan all files in the current directory. It will make a tree for every folder
// and the folder will have a subsection of files in the tree format. YES SORTING!
char **word;
int folder[1000];
int filecheck[1000];
int coun = 0;
int e = 0;
int f = 0;
int u = 0;
int p = 20;
void printdir(char *dir, int depth)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(dir)) == NULL)
{
fprintf(stderr,"cannot open directory: %s\n", dir);
return;
}
chdir(dir);
while((entry = readdir(dp)) != NULL)
{
lstat(entry->d_name,&statbuf);
if (S_ISDIR(statbuf.st_mode)) // Check if it's a directory
{
/* Found a directory, but ignore . and .. */
if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0)
{
continue;
}
word[coun] = malloc(strlen(entry->d_name) + 1);
folder[e] = coun; // Mark where the folder is.
e++;
f++;
strcpy(word[coun++], entry->d_name);
/* Recurse at a new indent level */
printdir(entry->d_name,depth+1);
f--;
}
else
{
if (f == 0)
{
filecheck[u] = coun;
u++;
}
word[coun] = malloc(strlen(entry->d_name) + 1);
strcpy(word[coun++], entry->d_name);
}
}
chdir("..");
closedir(dp);
}
int main(int argc, char* argv[])
{
word = calloc(1000, sizeof(*word));
printdir(".", 0);
printf("now, print the words in the order.\n");
int c = 0;
int l = 0;
for (int i = 0; i < coun; ++i) // Start readjusting the array in alphabetical order.
{
if (i == folder[c]) // If i matches with a folder, then increment and skip sorting.
{
c++;
}
else // If next is a file, then start the sorting.
{
int imin = i;
for (int j = i + 1; j < folder[c]; ++j) // Keep sorting until the folder ends.
{
char word1[1000], word2[1000];
strcpy(word1, word[j]);
strcpy(word2, word[imin]);
for(int p = 0; word1[p]; p++)
{
word1[p] = tolower(word1[p]);
}
for(int p = 0; word2[p]; p++)
{
word2[p] = tolower(word2[p]);
}
if (strcmp(word1, word2) < 0)
{
imin = j;
}
}
char *tmp = word[i];
word[i] = word[imin];
word[imin] = tmp;
}
}
c = 0;
l = 0;
printf(".\n");
for (int i = 0; i < coun; ++i) // Print the array in the specified format.
{
if (i == folder[c]) // If the folder is the same, print start of the folder.
{
printf("- %s\n", word[i]);
c++;
}
else if (i == filecheck[l]) // If it's a file, print the file normally.
{
printf("- %s\n", word[i]);
l++;
}
else // Print the file inside the folder.
{
printf(" - %s\n", word[i]);
}
}
exit(0);
}
我的程序应该以树格式打印当前目录。除了它正在检查的最终文件夹之外,它完美地完成了排序。出于某种原因,hw2 工作正常,但 hw1 没有排序。
now, print the words in the order.
.
- hw2
- find.c
- ls.c
- Makefile
- tree.c
- hw1
- grep.c
- factor.c
- uniq.c
- monster.c
- sort.c
- tree.c
- tree
我觉得我错过了一些东西,但我不知道如何让它发挥作用。我尝试了不同的方法,但没有奏效。有什么我可以做的吗?
解决方案
推荐阅读
- python - 将广度优先搜索的伪代码覆盖到python
- ruby-on-rails - Rails 升级抱怨旧的捆绑器版本
- git - 我无法将我的项目文件夹推送到 GitHub
- python - manim 0.12.0 中的散点图/线性回归
- tensorflow - TypeError: _logger_find_caller() 采用 0 到 1 个位置参数,但给出了 2 个
- jquery - 引导模式无法在 Rails 中打开
- pine-script - 如何绘制 MA 的斜率
- pandas - Matplotlib df.dataframe x 轴显示月份和日期
- javascript - 语法错误:)在 ejs 文件中的参数列表后丢失
- android - 构建共享当前用户操作/屏幕的 Android 应用程序功能