| | |
code for findfirstfile!
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jul 2007
Posts: 25
Reputation:
Solved Threads: 0
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
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 stringI 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
•
•
Join Date: Jul 2007
Posts: 25
Reputation:
Solved Threads: 0
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.
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
Besides, finddirstfile() is really only wildcards, not full regular expressions.
Or there's also the glob interface in GlibC
•
•
Join Date: Jul 2007
Posts: 25
Reputation:
Solved Threads: 0
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...
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...
OK, you've been a member for 6 months now, were you at any point planning to read the rules for posting code ?
•
•
Join Date: Jul 2007
Posts: 25
Reputation:
Solved Threads: 0
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.
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...
C Syntax (Toggle Plain Text)
#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; }
like if i give expression as "*nabil" or "?am*" then segmentation error is caused then
can any1 help me here now and fix this...
Bear in mind that if you type in
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.
Oh, and yes the code is better in that it is in code tags, but still looks pretty horrible because there is no indentation.
./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.
C Syntax (Toggle Plain Text)
$ echo *.c bar.c foo.c new.c util.c wrapper.c # Turn if off $ set -o noglob $ echo *.c *.c # Turn it back on $ set +o noglob $ echo *.c 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.
![]() |
Similar Threads
- Having trouble calling certain functions (C++)
- find files of certain type... (Assembly)
- Need help Writing a code which reads text files. (C++)
- Win32_FIND_DATA FindFirstFile (C++)
- Why is VC++ 2005 beta2 telling me this (C++)
- FindFirstFile & FindNextFile return extra files? (C++)
- Help with Searching files in Directories (C++)
- Visual c++ libraries (C++)
Other Threads in the C Forum
- Previous Thread: need help with single digit code
- Next Thread: sucinct example of hash table w/ chaining
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez graphics gtkgcurlcompiling gtkwinlinux hardware highest homework i/o ide inches initialization intmain() iso km license linked linkedlist linux linuxsegmentationfault list logical_drives loopinsideloop. lowest match matrix microsoft motherboard mqqueue multi mysql oddnumber odf open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string strings suggestions test testautomation unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






