SORTLIST.C
资源名称:C.rar [点击查看]
上传用户:qq5388545
上传日期:2022-07-04
资源大小:29849k
文件大小:1k
源码类别:
界面编程
开发平台:
C/C++
- #include <stdio.h>
- #include <dirent.h>
- #include <alloc.h>
- #include <string.h>
- #include <stdlib.h>
- void main(int argc, char *argv[])
- {
- DIR *directory_pointer;
- struct dirent *entry;
- struct FileList {
- char filename[64];
- struct FileList *next;
- } start, *node, *previous, *new;
- if ((directory_pointer = opendir(argv[1])) == NULL)
- printf("Error opening %sn", argv[1]);
- else
- {
- start.next = NULL;
- while (entry = readdir(directory_pointer))
- {
- // Find the correct location
- previous = &start;
- node = start.next;
- while ((node) && (strcmp(entry, node->filename) > 0))
- {
- node = node->next;
- previous = previous->next;
- }
- new = (struct FileList *) malloc(sizeof(struct FileList));
- if (new == NULL)
- {
- printf("Insufficient memory to store listn");
- exit(1);
- }
- new->next = node;
- previous->next = new;
- strcpy(new->filename, entry);
- }
- closedir(directory_pointer);
- node = start.next;
- while (node)
- {
- printf("%sn", node->filename);
- node = node->next;
- }
- }
- }