code for findfirstfile!

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2007
Posts: 25
Reputation: nabilchampion is an unknown quantity at this point 
Solved Threads: 0
nabilchampion nabilchampion is offline Offline
Light Poster

code for findfirstfile!

 
0
  #1
Jan 15th, 2008
hope all r doing fine..
i need a quick and very quick reply
m porting some cod e from windows to linux box..n i just encountered a function findfirstfile() used in windows..this function also takes wildcards as regular expression in the string
I searched the net for an alternate implementation in linux but hadnt got any! so m asking u guys tht if u have any idea or if someone has coded this function in linux then plz let me know... u can search findfirstfile() detail in MSDN
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,728
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: code for findfirstfile!

 
0
  #2
Jan 15th, 2008
You probably want to use opendir, readdir, and closedir. Ask man for more details.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: code for findfirstfile!

 
0
  #3
Jan 15th, 2008
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 25
Reputation: nabilchampion is an unknown quantity at this point 
Solved Threads: 0
nabilchampion nabilchampion is offline Offline
Light Poster

also need regular expression support!

 
0
  #4
Jan 16th, 2008
yes i have used those functions but the problem is that I couldnt find any builtin function in linux that can accept regular expressions as well... the windows function finddirstfile() accepts regular expression like "file*.txt", "?a*.c" ... i can use the linux functions for simple find first search by using dirent but i need to search in dirent structure with regular expression support as well... i want to know if there is any prebuilt function that at least searches with regular expression bcoz i dont want to write this function by myself if there exists any already.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: code for findfirstfile!

 
0
  #5
Jan 16th, 2008
Well you could always use a regex library on the Linux platform (there are several), to match the filenames read by readdir().

Besides, finddirstfile() is really only wildcards, not full regular expressions.

Or there's also the glob interface in GlibC
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 25
Reputation: nabilchampion is an unknown quantity at this point 
Solved Threads: 0
nabilchampion nabilchampion is offline Offline
Light Poster

Re: code for findfirstfile!

 
0
  #6
Jan 16th, 2008
thnx guys ... 'm near to end this..
infact i wasnt aware of regex b4 as i never needed it so 'm new to this..
i just wrote this code to test regex.h
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <regex.h>
int main(int argc,char **argv)
{
char str[20]="iamnabil";
int status;
regex_t myre;
regcomp(&myre, argv[1], REG_EXTENDED|REG_NOSUB);
status = regexec(&myre, str, (size_t)0, NULL, 0);
if(err==REG_NOMATCH)
printf("\n nOT matcheD\n");
else
if(err==0) printf("\nMatcHed !\n");
return 0;
}

it is working great but when i give * or ? wildcards at start of my RE string..it complains for segmentation fault..
like if i give expression as "*nabil" or "?am*" then segmentation error is caused then
can any1 help me here now and fix this...
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: code for findfirstfile!

 
0
  #7
Jan 16th, 2008
OK, you've been a member for 6 months now, were you at any point planning to read the rules for posting code ?
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 25
Reputation: nabilchampion is an unknown quantity at this point 
Solved Threads: 0
nabilchampion nabilchampion is offline Offline
Light Poster
 
0
  #8
Jan 16th, 2008
Sorry Salem... i do admit i never read those rules and just skimmed that now... i think now it is properly posted. Correct me if i am wrong any where.

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <string.h>
  4. #include <regex.h>
  5. int main(int argc,char **argv)
  6. {
  7. char str[20]="iamnabil";
  8. int status;
  9. regex_t myre;
  10. regcomp(&myre, argv[1], REG_EXTENDED|REG_NOSUB);
  11. status = regexec(&myre, str, (size_t)0, NULL, 0);
  12. if(err==REG_NOMATCH)
  13. printf("\n nOT matcheD\n");
  14. else
  15. if(err==0) printf("\nMatcHed !\n");
  16. return 0;
  17. }
it is working great but when i give * or ? wildcards at start of my RE string..it complains for segmentation fault..
like if i give expression as "*nabil" or "?am*" then segmentation error is caused then
can any1 help me here now and fix this...
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,878
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: code for findfirstfile!

 
0
  #9
Jan 16th, 2008
If it only get's that error if the FIRST char = '?' or '*'try checking if 'err' == REG_BADRPT . If so: report back.

Niek
Last edited by niek_e; Jan 16th, 2008 at 8:21 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: code for findfirstfile!

 
0
  #10
Jan 16th, 2008
Bear in mind that if you type in
./myprog *nabil at most Linux/Unix prompts, then the default is for the shell to have a go at expanding the wildcard expression to begin with. If that fails, then it's likely that your program will see argv[1] as being NULL, and that's where your segfault is coming from.

Perhaps check argc / argv before trying to use them in your code.

In bash, you can modify the shell's wildcard expansion, to allow such expressions to be passed to your program.
  1. $ echo *.c
  2. bar.c foo.c new.c util.c wrapper.c
  3. # Turn if off
  4. $ set -o noglob
  5. $ echo *.c
  6. *.c
  7. # Turn it back on
  8. $ set +o noglob
  9. $ echo *.c
  10. bar.c foo.c new.c util.c wrapper.c

Oh, and yes the code is better in that it is in code tags, but still looks pretty horrible because there is no indentation.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC