Thanks you guys..
I have one more question. I'm programming recursive directory traverse function.
It will be called from main program with absolute directory name.
I could printout in a simple 2~3 depth directory.
But I could not in complex structure.
(/lib/modules has two directory, and inside there, files and directories are gathered)
I think my program can not come back to called point.
I think chdir("..") or return is proper to process returning to called point.
Should I use depth counter or something else?
Thanks you.

void print_title(int oflag, char* output)
   {
	ofstream myout; 
	myout.open(output);

	if(oflag == 1) 
	   {
		cout << "Output file is " << output << endl;
		myout << "--------- Directory file information ---------" << endl;
	   }
	else
		cout << "--------- Directory file information ---------" << endl;
   }


void list_dir(char* dir1_nm)  //List directory function
{
	DIR *pdir;  //Open directory variable
	pdir = opendir(dir1_nm);
	int icount = 0;	//Count for file number
	char in_addr[255] = {0};	
	char dir2_nm[255];   //Passing subdirectory name
	char file_nm1[255];  //Making absolute path
	struct dirent *entry;   //Directory read structure 
	struct stat statbuf;    //File inode information structure
	strcpy(in_addr, dir1_nm); 
	if(chdir(dir1_nm) < 0)
		{
			printf("DIR : %s \n", dir1_nm);
			perror("chdir ");
			exit(1);
		}


	while ((entry = readdir(pdir)) != NULL)  
	{		
		stat(entry->d_name, &statbuf); //Using file name, getting link info	
		if (strcmp(entry->d_name, ".") == 0)  //If current directory, don't process
			continue;
		else if (strcmp(entry->d_name, "..") == 0) //Don't process parent directory
			continue;
		else if((entry->d_type) == DT_REG) //Process regular file
			{
				cout << "file_nm1 = " << getcwd(dir1_nm, 256) << "/" << entry->d_name << endl;
			}  //end else
		else if((entry->d_type) == DT_DIR)
	  		{
				cout << "directory is " << getcwd(dir1_nm, 256) << "/" << entry->d_name << endl;
				strcpy(dir2_nm, dir1_nm);
				strcat(dir2_nm, "/");
				strcat(dir2_nm, entry->d_name);	
				list_dir(dir2_nm);   //Recursive function call
			}
					
	closedir (pdir);   //Close reading input directory
       }
	//chdir("..");
return;
}

Its not necessary to actually change the current working directory. See this code snippet for example how to do that.

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.