我想写一个dir的程序,用c,怎么做?
这个可以吗,接分:)
#include
#include
#include
#inchlude
int j,num=0;
char ss[20],path[50],path2[50];
void main (int argc,char *argv[])
{
struct ffblk f;
int done;
if(argc==2) /*取命令行参数到数组中*/
strcpy(ss,argv[1]);
else
strcpy(ss,″*.*″); /*给数组赋值缺省参数*/
if (((ss[strlen(ss)-1]==′\\′||((ss[strlen(ss)-1]==:))
strcat(ss,″*.*″); /*若参数为路径或盘符,则加上″*.*″ */
getcwd(path1,50); /*取当前路径*/
if (chdir(ss)==0) /*判断参数是否为路径*/
strcat(ss,"\\*.*"); /*若路径末没有带"\",则加上"*.*" */
chdir(path1); /*恢复原来路径*/
strcpy(path2,ss);
for(j=strlen(path2);j>0;j--)/*提取参数中的路径到path2 */
{if((path2[j]==\\))||(path2[j]==:)){
path2[j+1]=\0;
goto senull;}
}
path2[0]=\0;
senull:
if(strlen(path2)==0) /* 若给出的参数中没带路径,则取当前路径*/
strcpy(path2,path1);
printf("\n**模拟DIR**\n 命令目录路径%s",path2);
done=findfirst(ss,&f,55); /*查找第一个配匹的文件*/
j=1;
while(!done)
{if (f.ff_attrib!=0x10) /* 若文件属性不是目录 */
printf("\n %15s %20ld",f.ff_name,f.ff_fsize);
else
printf("\n &11s
",f.ff_name);
num++;
j++;
if(j==23)
printf("\n --------More (按任意键继续)----");
getch();
j=0;
printf(″\n (目录路径%s)″,path2);}
done=findnext(&f); /*查找下一个配匹的文件*/
}
printf(″\n 当前目录中总共有%d个文件.\n″,num);