天天看點

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];

char ff_attrib;

int ff_ftime;

int ff_fdate;

long ff_fsize;

char ff_name[13];

}ff;

fp=fopen("c:\\1.txt","w+");

printf("Please Input Directory:\n");

gets(str1);

k = chdir(str1);

if (k!= 0)

{

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

getch();

exit(-1);

}

printf("\nPlease Input File Type:\n");

gets(str2);

i = findfirst(str2,&ff,FA_ARCH);

if(i!=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)

{

j = findnext(&ff);

if (j ==0)

{

printf("%s\n",ff.ff_name);

fputs(ff.ff_name,fp);

fputs("\n",fp);}

}

}

fclose(fp);

getch();

}