34 lines
946 B
C
34 lines
946 B
C
#include "c_FileIter.h"
|
|||
|
|
#include <stdlib.h>
|
||
|
|
#include <stdio.h>
|
||
|
|
|
||
|
|
|
||
|
|
int main() {
|
||
|
|
c_FileIter_t iter;
|
||
|
|
|
||
|
|
// 初始化迭代器,目标为当前目录 "."
|
||
|
|
if (c_FileIter_Init(&iter, ".") == C_ERR_OK) {
|
||
|
|
printf("开始遍历目录文件:\n");
|
||
|
|
|
||
|
|
// 使用经典的迭代器标准循环结构
|
||
|
|
while (c_FileIter_HasNext(&iter)) {
|
||
|
|
const c_FileEntry_t* entry = c_FileIter_Get(&iter);
|
||
|
|
if (entry) {
|
||
|
|
if (entry->type == C_FILE_TYPE_DIR) {
|
||
|
|
printf("[DIR] %s\n", entry->name);
|
||
|
|
} else {
|
||
|
|
printf("[FILE] %-25s 大小: %llu 字节\n", entry->name, entry->size);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 推进到下一个文件
|
||
|
|
c_FileIter_Next(&iter);
|
||
|
|
}
|
||
|
|
printf("遍历结束,资源已自动安全回收。\n");
|
||
|
|
} else {
|
||
|
|
printf("无法初始化目录迭代器!\n");
|
||
|
|
}
|
||
|
|
|
||
|
|
return 0;
|
||
|
|
}
|