943,866 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 6622
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 15th, 2008
0

code for findfirstfile!

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
nabilchampion is offline Offline
25 posts
since Jul 2007
Jan 15th, 2008
0

Re: code for findfirstfile!

You probably want to use opendir, readdir, and closedir. Ask man for more details.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 15th, 2008
0

Re: code for findfirstfile!

Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jan 16th, 2008
0

also need regular expression support!

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
nabilchampion is offline Offline
25 posts
since Jul 2007
Jan 16th, 2008
0

Re: code for findfirstfile!

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
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jan 16th, 2008
0

Re: code for findfirstfile!

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...
Reputation Points: 10
Solved Threads: 0
Light Poster
nabilchampion is offline Offline
25 posts
since Jul 2007
Jan 16th, 2008
0

Re: code for findfirstfile!

OK, you've been a member for 6 months now, were you at any point planning to read the rules for posting code ?
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jan 16th, 2008
0
Re: code for findfirstfile!
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...
Reputation Points: 10
Solved Threads: 0
Light Poster
nabilchampion is offline Offline
25 posts
since Jul 2007
Jan 16th, 2008
0

Re: code for findfirstfile!

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 Nick Evan; Jan 16th, 2008 at 8:21 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Jan 16th, 2008
0

Re: code for findfirstfile!

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: need help with single digit code
Next Thread in C Forum Timeline: sucinct example of hash table w/ chaining





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC