I am trying to make dir command .I have a problem in my code.it print name of all subdirectories and files but it does not print all files of all subdirectories only print one subdirectories files.
I want to print all file and all subdirectory files

Void list(const char *path)
{
DIR *d,*d1;
struct dirent *dir;
chdir(path);
d=opendir(".");
if(d)
while((dir=readdir(d)))
if(!strcmp(".",dir->d_name))
continue;
if(!strcmp("..",dir->d_name))
continue;
else
{
printf("%s\n",dir->d_name);
d1=opendir(dir->d_name);
if(d1)
{
list(dir->d_name);
}
}
closedir(d);
}


int main()
{
char path[]="c:\\";
list(path);
}

What compiler are you using? Looks like os is MS-Windows but using *nix opendir() and associated functions.

In any event, you don't need to use chdir(). Just call list() recursively for each subdirectory that's encountered.

What compiler are you using? Looks like os is MS-Windows but using *nix opendir() and associated functions.

In any event, you don't need to use chdir(). Just call list() recursively for each subdirectory that's encountered.

you are right.i am using windows xp and dev c++ compiler.
i am not getting what are you saying.should i use another function because opendir is for *nix os.

I've only seen it used with *nix compilers, but if yours supports it then it should be ok. The main thing is to use a recursive function call when a directory is encountered.

I've only seen it used with *nix compilers, but if yours supports it then it should be ok. The main thing is to use a recursive function call when a directory is encountered.

thanks ancient dragon for your help and i want some more help.

I did this program with findfirstfile and findnextfile it run well.
But in it i have to give a path so i modified it and add _getdrives function.
Problem is that it returns a charecter and path should be a string so what i do?
Int main()
{
char drive='a';
int get=_getdrives();
while(get)

{
if(get & 1)
{
path=drive;/*it is error*/
list(path);/*and this i want to do it*/
drive++;
}

drive>>=1;
}
}

I guess that _getdirves() reutnrs a bit mapped integer of the drive letters on the computer. So that the first bit is drive 'A', the second is drive 'B', etc.

char path[255] = {0};
unsigned int get = _getdrives();
for(int i 1; i < 26; i++)
{
   if( get & 1 )
   {
        path[0] = 'A' + (i-1);
        strcat(path,":\\*.*);
        // do other stuff here
   }
   get >>= 1;
}

I guess that _getdirves() reutnrs a bit mapped integer of the drive letters on the computer. So that the first bit is drive 'A', the second is drive 'B', etc.

char path[255] = {0};
unsigned int get = _getdrives();
for(int i 1; i < 26; i++)
{
   if( get & 1 )
   {
        path[0] = 'A' + (i-1);
        strcat(path,":\\*.*);
        // do other stuff here
   }
   get >>= 1;
}

thanks for your help
if you don't mind than i have little more difficulty that at drive a: system shows a error that drive is not ready for use,please insert the disk.
What can i do for remove it.

put a disk in the drive :)

put a disk in the drive :)

my question is that , there is any way that detects drive is ready for use or not.

I don't know. There was a way under MS-DOS 6.X. I thought M$ may have made it a Windows Hook, but I don't see anything that would be useful to you there.

You might read these links

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.