用c语言实现在6818开发板上浏览用户指定的目录下的图片支持目录内文件过滤一个目录内有多种类型的文件过滤掉非jpg格式的文件用链表将文件与链表对应起来
以下是使用C语言实现在6818开发板上浏览用户指定目录下的图片,并使用链表将文件与链表对应起来的示例代码:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
// 定义链表结构
typedef struct FileNode {
char filename[256];
struct FileNode* next;
} FileNode;
// 检查文件是否是jpg格式
int isJpgFile(const char* filename) {
const char* extension = strrchr(filename, '.');
if (extension != NULL && strcmp(extension, ".jpg") == 0) {
return 1;
}
return 0;
}
// 打印链表中的文件名
void printFileList(FileNode* head) {
FileNode* current = head;
while (current != NULL) {
printf("%s\n", current->filename);
current = current->next;
}
}
// 释放链表内存
void freeFileList(FileNode* head) {
FileNode* current = head;
while (current != NULL) {
FileNode* temp = current;
current = current->next;
free(temp);
}
}
// 浏览目录并过滤非jpg文件
FileNode* browseDirectory(const char* directory) {
DIR* dir;
struct dirent* entry;
FileNode* head = NULL;
FileNode* tail = NULL;
dir = opendir(directory);
if (dir == NULL) {
printf("无法打开目录\n");
return NULL;
}
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_REG && isJpgFile(entry->d_name)) {
FileNode* newNode = (FileNode*)malloc(sizeof(FileNode));
strcpy(newNode->filename, entry->d_name);
newNode->next = NULL;
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
}
closedir(dir);
return head;
}
int main() {
const char* directory = "/path/to/directory"; // 替换为用户指定的目录路径
FileNode* fileList = browseDirectory(directory);
printFileList(fileList);
freeFileList(fileList);
return 0;
}
请将代码中的/path/to/directory替换为用户指定的目录路径。在浏览用户指定的目录下的图片时,该代码将仅显示扩展名为.jpg的文件,并使用链表将文件与链表对应起来。函数browseDirectory()负责浏览目录并过滤非jpg文件,函数printFileList()打印链表中的文件名,freeFileList()释放链表内存。
请注意,以上代码仅提供了一个示例实现,可能需要根据具体的应用场景进行修改和优化
原文地址: http://www.cveoy.top/t/topic/iGiN 著作权归作者所有。请勿转载和采集!