c语言怎么在程序中读取文件夹,用C语言获取目录中的文件列表

#include "stdio.h"

#include "dos.h"

#include "conio.h"

main()

{

char str1[100];/*保存路径*/

char str2[100];/*保存查找类型*/

int i,j=0,k; /*函数返回值*/

FILE *fp; /*文件指针*/

struct ffblk /*文件结构*/

{

char ff_reserved[21]; /*DOS保留字*/

char ff_attrib; /*文件属性*/

int ff_ftime; /*文件时间*/

int ff_fdate; /*文件日期*/

long ff_fsize; /*文件长度*/

char ff_name[13]; /*文件名*/

}ff;

fp=fopen("c:\\1.txt","w+"); /*打开文件C盘根下的1.txt*/

printf("Please Input Directory:\n"); /*提示输入目录*/

gets(str1); /*得到输入串*/

k = chdir(str1); /*切换当前工作目录,正确返回0*/

if (k!= 0) /*如果不等于0,提示信息后退出*/

{

printf("Change Path failure!\n");

getch();

exit(-1);

}

printf("\nPlease Input File Type:\n");/*提示输入文件类型*/

gets(str2); /*得到输入串*/

i = findfirst(str2,&ff,FA_ARCH); /*查找匹配条件的第一个文件,正确返回0,FA_ARCH 档案文件*/

if(i!=0) /*如果返回值不为0,提示信息后退出*/

{

printf("File Not Find!\n");

getch();

exit(-1);

}

else

{

printf("\n");

printf("%s\n",ff.ff_name); /*在屏幕上打印*/

fputs(ff.ff_name,fp); /*输出到文件*/

fputs("\n",fp); /*输出换行符到文件*/

while(j==0) /*循环查找,直到findnext不等于0*/

{

j = findnext(&ff); /*查找与findfirst相匹配的文件*/

if (j ==0) /*如果找到*/

{

printf("%s\n",ff.ff_name); /*在屏幕上打印*/

fputs(ff.ff_name,fp); /*输出到文件*/

fputs("\n",fp);} /*输出换行符到文件*/

}

}

fclose(fp); /*关闭文件指针*/

getch(); /*等待键盘按键*/

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的C语言程序,以获取目录最早的文件夹名称。程序使用冒泡排序算法进行排序。 ```c #include <stdio.h> #include <stdlib.h> #include <dirent.h> #include <sys/stat.h> #include <string.h> void swap(char **str1, char **str2){ char *temp = *str1; *str1 = *str2; *str2 = temp; } int main(){ DIR *dir; struct dirent *entry; struct stat statbuf; char **dir_list; int i, j, count = 0; // 打开目录 if((dir = opendir(".")) == NULL){ perror("opendir"); exit(1); } // 统计目录文件夹数目 while((entry = readdir(dir)) != NULL){ if(entry->d_type == DT_DIR && strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) count++; } // 为文件夹列表动态分配内存 dir_list = (char **)malloc(count * sizeof(char *)); for(i = 0; i < count; i++) dir_list[i] = (char *)malloc(sizeof(char) * 256); // 读取目录文件夹名称 rewinddir(dir); i = 0; while((entry = readdir(dir)) != NULL){ if(entry->d_type == DT_DIR && strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0){ strcpy(dir_list[i], entry->d_name); i++; } } // 冒泡排序 for(i = 0; i < count - 1; i++){ for(j = 0; j < count - i - 1; j++){ if(strcmp(dir_list[j], dir_list[j+1]) > 0) swap(&dir_list[j], &dir_list[j+1]); } } // 输出排序后的文件夹名称 printf("目录最早的文件夹名称为:%s\n", dir_list[0]); // 释放动态分配的内存 for(i = 0; i < count; i++) free(dir_list[i]); free(dir_list); // 关闭目录 closedir(dir); return 0; } ``` 程序通过 opendir() 函数打开当前目录,然后使用 readdir() 函数遍历目录的所有文件文件夹,统计出文件夹的数目。然后动态分配内存,将文件夹名称保存到数组。使用冒泡排序算法对文件夹名称进行排序,最后输出排序后的第一个元素即为最早的文件夹名称。 需要注意的是,程序使用了 dirent.h 和 sys/stat.h 头文件,需要链接对应的库文件。在 Linux 平台下,可以使用以下命令编译程序: ``` gcc -o sort_directory sort_directory.c -ldir ``` 在 Windows 平台下,可以使用以下命令编译程序: ``` gcc -o sort_directory sort_directory.c ``` 希望能对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值