Searching file

Is there any way to make a program that will search a file on the disk.The disk here includes all my computer...
I've just written a function that recursively scans a directory for its content, i hope this helps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

int ParseDirectory(string dir) {
	DIR *tDir;
	
	tDir = opendir(dir.c_str());
	if(tDir == nullptr) {
		cerr << endl << "Error opening directory " << dir 
			 << " (errno: " << errno << ")" << endl;
		return errno;
	}
	
	struct dirent *dirP;
	struct stat filestat;
	string path;
	while( (dirP = readdir(tDir)) ) {
		//Skip current object if it is this directory or parent directory
		if(!strncmp(dirP->d_name, ".", 1) || !strncmp(dirP->d_name, "..", 2))
			continue;
		
		if(dir==".") path = dirP->d_name;
		else		 path = dir + "/" + dirP->d_name;
		
		//Skip current file / directory if it is invalid in some way
		if(stat(path.c_str(), &filestat)) continue;
		
		//Recursively call this function if current object is a directory
		if(S_ISDIR(filestat.st_mode)) {
			ParseDirectory(path);
			continue;
		}
		
		//At this position you can check if the current file (path) is the file you are searching
	}
	
	closedir(tDir);
	
	return 0;
}


(It was written for a unix filesystem)
thanks, but can u give a little explaination about the function
i thought it's self-explanatory, well there is not much to say.

It takes the directory dir where you want to start as a parameter (in your case that would be the path of the topmost directory on the disk) and the return value indicates whether there was an error(!=0) or not(==0).

At first it opens the directory dir and then it uses a depth-first search to parse the directory and sub-directories recursively. You'll have to remove line 20-22 if you're using windows and line 24-25 can be simplified to path = dir + "/" + dirP->d_name; in this case.
Maybe you'll need to include other headers in windows aswell, i dont know much about that.
@AleaIactaEst Thanks a lot....
That did not help you? Problem still unsolved?
actually i'm working on a project and I have time to come up to this function...I
hav'nt tried your program but have got your concept.Thanx again
Topic archived. No new replies allowed.