The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
finding a file in disk with C/C++
Discuss finding a file in disk with C/C++ in the C Programming forum on Dev Shed. finding a file in disk with C/C++ C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 18th, 2002, 09:08 PM
|
|
Junior Member
|
|
Join Date: Mar 2002
Location: Istanbul / Turkey
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
finding a file in disk with C/C++
i'm trying to code a function which will be responsible for doing the following thing as fast as possible and with minimum resources;
i'll send it a file name like "foo.bar" and a directory like "/home/dummy " (to let recursion if needed) and it'll search the directory and all subdirectories and return a char ** as a result which will contain the file locations, such as "/home/dummy/bin/foo.bar" etc..
To do this i have some thing in my mind but i am scared to use it because of my past experience with protected mode and assembly.. i'm decideing to use int 21h with function 4Eh to check if file exists or not..
In oder hand i know that i may use fopen to text if file exists but it'll be quite slow for my purpose with that function.
And finding subdirectories still remains obscure.. the code have to be portable and this closes all of my narrow escapes..
I need something similar to Midnight Commander's "search for files" feature.. But i dont know how it'd be as fast as MC is, or how ro code such a thing in a limited time.
What i need is documentation or your ideas about how do it efficient.. i'd prefer C but C++ is wellcome.
Can anyone lead me to find out how i can code that function?
|

June 19th, 2002, 08:27 AM
|
 |
/(bb|[^b]{2})/
|
|
Join Date: Nov 2001
Location: Somewhere in the great unknown
|
|
|
Have you checked out the dirent.h file? It deals with the majority of director handling.
|

June 19th, 2002, 11:01 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
Are you coding this for DOS or *NIX. The reason I'm asking is because your path names indicate an *NIX environment, but the INT 21h statement indicates a DOS environment. Also, I'm curious exactly how much programming you've done in assembly for DOS, because a moderately experienced programmer would know that INT21h is the system call mechanism for DOS alone and doesn't apply to other OSs.
Anyways, assuming you have a *NIX environment, here's some C code that I wrote to go down thru a directory tree and do things to files. I've only snipped certain sections that you can easily figure out for yourself.
Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
void crawldir(char *currdir);
void StripFile(char *filename, long filesize);
char tempname[MAXNAMLEN];
int main(int argc, char **argv) {
if (argc < 2) {
fprintf(stderr, "Usage: %s startdir\n", argv[0]);
fprintf(stderr, "E.g. %s /var/www/foo\n", argv[0]);
fprintf(stderr, "Description:\t This program ... <snip>.\n");
fprintf(stderr, "\t\t It recursively crawls its way up startdir. \n");
return 0;
}
crawldir(argv[1]);
}
void crawldir(char *currdir)
{
/* Open the directory */
DIR *dir;
struct dirent *de;
struct stat st;
chdir (currdir);
printf("Current dir is %s\n", getwd(tempname));
dir = opendir(".");
while (de = readdir(dir)) {
lstat(de->d_name, &st);
if (S_ISDIR(st.st_mode)) {
if ((strcmp(de->d_name, ".") != 0) && (strcmp(de->d_name, "..") != 0)) {
crawldir(de->d_name);
}
}
else {
StripFile(de->d_name, st.st_size);
}
}
closedir(dir);
printf("Switching back one dir level\n");
chdir("..");
}
void StripFile(char *filename, long filesize) {
...<snip>... your code comes here
}
Hope this helps!
|

June 19th, 2002, 11:21 PM
|
|
Junior Member
|
|
Join Date: Mar 2002
Location: Istanbul / Turkey
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
yeah, you're right.. havent done much assembly coding before.
i think your example will do much of what i have been looking for.. thank you for your help. 
|

June 19th, 2002, 11:31 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
Edited the above code to remove hardcoded array sizes and use constants.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|