[英]save file listing into array or something else C
我有這個功能
void file_listing(){
DIR *dp;
struct stat fileStat;
struct dirent *ep;
int file=0;
dp = opendir ("./");
if (dp != NULL){
while ((ep = readdir(dp))){
char *c = ep->d_name;
char d = *c; /* first char */
if((file=open(ep->d_name,O_RDONLY)) < -1){
perror("Errore apertura file");
}
if(fstat(file,&fileStat)){ /*file info */
perror("Errore funzione fstat");
}
if(S_ISDIR(fileStat.st_mode)){ /* directory NOT listed */
continue;
}
else{
if(d != '.'){ /* if filename DOESN'T start with . will be listed */
"save into someting" (ep->d_name);
}
else{
continue; /* altrimenti non lo listo */
}
}
}
(void) closedir (dp);
}
else{
perror ("Impossibile aprire la directory");
return 1;
}
}
我想將文件列表的結果保存到數組,結構或列表等中,但是我不知道該怎么做。
提前致謝!
這是一個示例函數,該函數將文件列表存儲在數組中並返回條目數:
#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <malloc.h>
size_t file_list(const char *path, char ***ls) {
size_t count = 0;
size_t length = 0;
DIR *dp = NULL;
struct dirent *ep = NULL;
dp = opendir(path);
if(NULL == dp) {
fprintf(stderr, "no such directory: '%s'", path);
return 0;
}
*ls = NULL;
ep = readdir(dp);
while(NULL != ep){
count++;
ep = readdir(dp);
}
rewinddir(dp);
*ls = calloc(count, sizeof(char *));
count = 0;
ep = readdir(dp);
while(NULL != ep){
(*ls)[count++] = strdup(ep->d_name);
ep = readdir(dp);
}
closedir(dp);
return count;
}
int main(int argc, char **argv) {
char **files;
size_t count;
int i;
count = file_list("/home/rgerganov", &files);
for (i = 0; i < count; i++) {
printf("%s\n", files[i]);
}
}
請注意,我在目錄上進行了兩次迭代-第一次獲取文件計數,第二次保存結果。 如果在執行過程中在目錄中添加/刪除文件,此操作將無法正常進行。
另一個例子,更干凈,但不是線程安全的,因為readdir()
不是:
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
static void *realloc_or_free(void *ptr, size_t size) {
void *tmp = realloc(ptr, size);
if (tmp == NULL) {
free(ptr);
}
return tmp;
}
static int get_dirent_dir(char const *path, struct dirent **result,
size_t *size) {
DIR *dir = opendir(path);
if (dir == NULL) {
closedir(dir);
return -1;
}
struct dirent *array = NULL;
size_t i = 0;
size_t used = 0;
struct dirent *dirent;
while ((dirent = readdir(dir)) != NULL) {
if (used == i) {
i += 42; // why not?
array = realloc_or_free(array, sizeof *array * i);
if (array == NULL) {
closedir(dir);
return -1;
}
}
array[used++] = *dirent;
}
struct dirent *tmp = realloc(array, sizeof *array * used);
if (tmp != NULL) {
array = tmp;
}
*result = array;
*size = used;
closedir(dir);
return 0;
}
static int cmp_dirent_aux(struct dirent const *a, struct dirent const *b) {
return strcmp(a->d_name, b->d_name);
}
static int cmp_dirent(void const *a, void const *b) {
return cmp_dirent_aux(a, b);
}
int main(void) {
struct dirent *result;
size_t size;
if (get_dirent_dir(".", &result, &size) != 0) {
perror("get_file_dir()");
}
qsort(result, size, sizeof *result, &cmp_dirent);
for (size_t i = 0; i < size; i++) {
printf("%s\n", result[i].d_name);
}
free(result);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.