如何使用c中的系统调用根据当前目录的大小按升序显示文件名?
how to display the filenames in ascending order according to their sizes of current directory using system call in c?
我正在尝试使用 c 中的系统调用根据文件名的大小对文件名进行排序。
我试过了...
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <limits.h>
#include <string.h>
int main() {
DIR *dir1, *dir2;
int fd, cur_size, min_size;
struct dirent *dirent1, *dirent2;
struct stat st1, st2;
char min_file_name[1000];
char filename[1000];
dir1 = opendir(".");
while ((dirent1 = readdir(dir1)) != NULL) {
min_size = INT_MAX;
dir2 = opendir(".");
while ((dirent2 = readdir(dir2)) != NULL) {
stat(dirent2->d_name, &st1);
cur_size = st1.st_size;
strcpy(filename, dirent2->d_name);
if (cur_size <= min_size && strcmp(min_file_name, filename) != 0) {
min_size = cur_size;
strcpy(min_file_name, dirent2->d_name);
}
}
printf("File name = %s || size = %d \n", min_file_name, min_size);
}
}
输出
amol@amol-Ideapad-320:~/AOS$ gcc q3.c
amol@amol-Ideapad-320:~/AOS$ ./a.out
File name = || size = 0
File name = || size = 0
File name = || size = 0
File name = || size = 0
恐怕我不明白你的嵌套循环方法中的逻辑。
要按给定顺序生成列表,您可以构建目录中条目的排序列表并打印它。
这是一个在链表上使用简单的二次插入排序的修改版本:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
struct entry {
char *name;
long long int size;
struct entry *next;
};
int main() {
DIR *dir;
struct dirent *dp;
struct stat st;
struct entry *head = NULL, *ep, **npp;
dir = opendir(".");
if (dir == NULL) {
perror("cannot open directory");
return 1;
}
while ((dp = readdir(dir)) != NULL) {
ep = calloc(sizeof(*ep), 1);
if (ep == NULL) {
perror("cannot allocate memory");
return 1;
}
ep->name = strdup(dp->d_name);
if (ep->name == NULL) {
perror("cannot allocate memory");
return 1;
}
if (!stat(dp->d_name, &st)) {
ep->size = st.st_size;
}
for (npp = &head; *npp && (*npp)->size <= ep->size; npp = &(*npp)->next)
continue;
ep->next = *npp;
*npp = ep;
}
closedir(dir);
for (ep = head; ep; ep = ep->next) {
printf("%s\n", ep->name);
}
while ((ep = head) != NULL) {
head = ep->next;
free(ep->name);
free(ep);
}
return 0;
}
我正在尝试使用 c 中的系统调用根据文件名的大小对文件名进行排序。
我试过了...
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <limits.h>
#include <string.h>
int main() {
DIR *dir1, *dir2;
int fd, cur_size, min_size;
struct dirent *dirent1, *dirent2;
struct stat st1, st2;
char min_file_name[1000];
char filename[1000];
dir1 = opendir(".");
while ((dirent1 = readdir(dir1)) != NULL) {
min_size = INT_MAX;
dir2 = opendir(".");
while ((dirent2 = readdir(dir2)) != NULL) {
stat(dirent2->d_name, &st1);
cur_size = st1.st_size;
strcpy(filename, dirent2->d_name);
if (cur_size <= min_size && strcmp(min_file_name, filename) != 0) {
min_size = cur_size;
strcpy(min_file_name, dirent2->d_name);
}
}
printf("File name = %s || size = %d \n", min_file_name, min_size);
}
}
输出
amol@amol-Ideapad-320:~/AOS$ gcc q3.c
amol@amol-Ideapad-320:~/AOS$ ./a.out
File name = || size = 0
File name = || size = 0
File name = || size = 0
File name = || size = 0
恐怕我不明白你的嵌套循环方法中的逻辑。
要按给定顺序生成列表,您可以构建目录中条目的排序列表并打印它。
这是一个在链表上使用简单的二次插入排序的修改版本:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
struct entry {
char *name;
long long int size;
struct entry *next;
};
int main() {
DIR *dir;
struct dirent *dp;
struct stat st;
struct entry *head = NULL, *ep, **npp;
dir = opendir(".");
if (dir == NULL) {
perror("cannot open directory");
return 1;
}
while ((dp = readdir(dir)) != NULL) {
ep = calloc(sizeof(*ep), 1);
if (ep == NULL) {
perror("cannot allocate memory");
return 1;
}
ep->name = strdup(dp->d_name);
if (ep->name == NULL) {
perror("cannot allocate memory");
return 1;
}
if (!stat(dp->d_name, &st)) {
ep->size = st.st_size;
}
for (npp = &head; *npp && (*npp)->size <= ep->size; npp = &(*npp)->next)
continue;
ep->next = *npp;
*npp = ep;
}
closedir(dir);
for (ep = head; ep; ep = ep->next) {
printf("%s\n", ep->name);
}
while ((ep = head) != NULL) {
head = ep->next;
free(ep->name);
free(ep);
}
return 0;
}