C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old June 18th, 2002, 09:08 PM
plastique plastique is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Location: Istanbul / Turkey
Posts: 2 plastique User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #2  
Old June 19th, 2002, 08:27 AM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 4,834 Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 23 h 30 m 30 sec
Reputation Power: 88
Send a message via ICQ to Onslaught
Have you checked out the dirent.h file? It deals with the majority of director handling.

Reply With Quote
  #3  
Old June 19th, 2002, 11:01 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,432 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 22 h 29 m 51 sec
Reputation Power: 784
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!

Reply With Quote
  #4  
Old June 19th, 2002, 11:21 PM
plastique plastique is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Location: Istanbul / Turkey
Posts: 2 plastique User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #5  
Old June 19th, 2002, 11:31 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,432 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 22 h 29 m 51 sec
Reputation Power: 784
Edited the above code to remove hardcoded array sizes and use constants.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > finding a file in disk with C/C++


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway