| | |
Function to find number of files in a directory
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 40
Reputation:
Solved Threads: 2
Hello guys,
I am doing a small C/C++ program(mainly for use in my father's work - Includes a batch processing inside a folder which is repetitive).
I just need to find the number of files in a folder(not necessarily of a single filetype). What I thought was using
Can somebody suggest me a simple function(NOT MFC), to do count no. of files in a given folder.
- Appreciate your help,
Thanks
I am doing a small C/C++ program(mainly for use in my father's work - Includes a batch processing inside a folder which is repetitive).
I just need to find the number of files in a folder(not necessarily of a single filetype). What I thought was using
system("dir > TmpFile") , and checking in that file. The code for checking in this goes quite complex. Can somebody suggest me a simple function(NOT MFC), to do count no. of files in a given folder.
- Appreciate your help,
Thanks
Read here http://www.adrianxw.dk/SoftwareSite/...irstFile1.html (and part 2 and 3), and in the retrieving loop you could simply have a counter that's returned by the function.
•
•
•
•
This code is wrong (no recursive, no reparse points, etc)
See the official MS sample (SDK)
To be honest Marco, your posts are getting a bit boring. You always post something like: 'This sux, use win32 API' and sometimes you add a link to somesort of crappy newsgroup.
Don't call something wrong if you can't explain why.
Last edited by niek_e; May 14th, 2009 at 9:24 am.
•
•
Join Date: Oct 2009
Posts: 4
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
/* SearchEngine.cpp : Creating Search Index and perform searching with the given input term(S) */ #include <stdio.h> #include <windows.h> #include <string.h> #include <conio.h> #include <stdlib.h> #define HASHSIZE 101 typedef struct _Document Document; typedef struct _WordList WordList; typedef struct _Position Position; /* Hold positions of words in file */ struct _Position { long pos; int flag; Position *next; }; /* Hold list of Documents */ struct _Document { char *docname; int count; Position *wordposition; Document *next; }; /* Hold list of words */ struct _WordList { char *name; WordList *next; Document *doc; }; WordList* wordTable[HASHSIZE]; int main(int argc, char * argv[]) { char *dirName; char key[256]; void performDirectoryScan(char *dirName); void createSearchIndex(char *dirName,char *fileName); void initWordTable(); unsigned int hash(char *word); WordList* lookup(char *word); char* getStringDuplicate(char *words); char* getNextWord(char *key); Document* getNewDocument(TCHAR *fileName); Document* getDocumentList(char *word); void insertToWordTable(char* name,TCHAR *filename); void insertWordPosition(Document* docName,long wordpos); void findWordPosition(Position* fposList,Position *tposList); void processDocumentList(Document *fdocList,char *word); void printDocumentList(Document *docList); void doSearch(char *key); void displayTable(); void cleanup(); dirName=(char *)malloc((strlen(argv[1])+ 1) ); strcpy(dirName,argv[1]); strcat(dirName,"*.*"); initWordTable(); performDirectoryScan(dirName); displayTable(); printf("\n Enter Search term :"); scanf("%s",key); doSearch(key); cleanup(wordTable); getch(); return 0; } /* Initialize the word table */ void initWordTable() { int i; for(i=0;i<HASHSIZE;i++) wordTable[i]=NULL; } /* return hash value */ unsigned int hash(char words[]){ unsigned int h=0; int i; for( i=0 ; words[i] != NULL ; i++) h=words[i]+h*31; return h%HASHSIZE; } char* getStringDuplicate(char *words){ int l=strlen(words)+1; char *dupStr=(char*)malloc(l*sizeof(char)); strcpy(dupStr,words); if(dupStr==NULL) return NULL; else return dupStr; } /* Scanning all files from Directory */ void performDirectoryScan(char *dirName) { WIN32_FIND_DATA info; int i=0; HANDLE h = FindFirstFile(dirName,&info); if (h == INVALID_HANDLE_VALUE) { return; } do { if (strcmp(info.cFileName, ".") != 0 && strcmp(info.cFileName, "..") != 0) { createSearchIndex(dirName,info.cFileName,h); } } while (FindNextFile(h, &info)); FindClose(h); } /* Creating Indexer using words scanned */ void createSearchIndex(char *dirName,char *fileName,HANDLE h) { WordList *wl; FILE *inputFile; char ch,*words; int i=0,j=0; int length=0; long wordpos; wl=(WordList *)malloc(sizeof(WordList)); words=(char *)malloc((GetFileSize(h,fileName))*sizeof (char)); strcat(dirName,fileName); inputFile = fopen(dirName,"r"); if (inputFile==NULL) { printf("File does not exist"); return; } ch = fgetc (inputFile); while (ch != EOF) { wordpos=ftell(inputFile); *(words+i) = ch; if(ch == ' ') { *(words+i) = '\0'; wordpos=(wordpos-strlen(words)) + 1; insertToWordTable(wordTable,&words,wordpos,fileName); i=0; j++; length++; } ch = fgetc(inputFile); } insertToWordTable(&words,fileName); fclose (inputFile); } /* Look up routine to scan words that are hashed */ WordList* lookup(char *words){ unsigned int hi=hash(words); WordList *wl=wordTable[hi]; for ( ; wl!=NULL ; wl=wl->next ) { if(!strcmp(wl->name,words)) return wl; } wl=(WordList*)malloc(sizeof(WordList)); if (wl==NULL) { printf("\n Cannot allocate memory for wordlist \n"); return; } wl->name=getStringDuplicate(words); wl->doc=NULL; wl->next=wordTable[hi]; wordTable[hi]=wl; return wl; } /* Returns new position of given word */ void insertWordPosition(Document* docName,long wordpos) { Position *curpos,*newpos; newpos=(Position *)malloc(sizeof(Position)); newpos->pos=wordpos; newpos->flag=0; newpos->next=NULL; curpos=docName->wordposition; while(curpos->next != NULL) { curpos=curpos->next; } curpos->next=newpos; } /* returns a new document */ Document* getNewDocument(char *fileName) { char *str; Document *newdoc; newdoc=(Document *)malloc(sizeof (Document) ); newdoc->count=0; newdoc->docname=(char *)malloc(sizeof (char)); strcpy(newdoc->docname,fileName); newdoc->count++; newdoc->wordposition=NULL; newdoc->next=NULL; return newdoc; } /* Insert the term into hashtable based on hashvalue */ void insertToWordTable(char **words,long wordpos,char *fileName){ unsigned int hi; WordList* wl; char *str; Position *temppos,*newpos; Document *tempdoc,*newdoc,*prevdoc,*curdoc; wl=lookup(*words,wordTable); curdoc=wl->doc; prevdoc=curdoc; temppos=curdoc->wordposition; while ( curdoc != NULL ) { if(wcscmp(curdoc->docname,fileName)==NULL){ curdoc->count++; insertWordPosition(curdoc,wordpos); return; } prevdoc=curdoc; curdoc=curdoc->next; } newdoc=getNewDocument(fileName); if(wl->doc==NULL){ wl->doc=newdoc; return; } prevdoc->next=newdoc; insertWordPosition(newdoc,wordpos); return; } /* Display Contents of the table */ void displayTable(){ int i; WordList *wl; Document *tempdoc; printf("\nWords In the Hash Table are:\n"); for ( i=0 ; i<HASHSIZE ; i++){ wl=wordTable[i]; for ( ; wl!=NULL ; wl=wl->next ) { printf("\n \nWord :%s \n",wl->name); for ( tempdoc=wl->doc ; tempdoc!=NULL ; tempdoc=tempdoc->next ) { printf("\nDocument name is: %s",tempdoc->docname); printf("\n Count : %d",tempdoc->count); } } } } /* get all the words from input key */ char* getNextWord(char *key) { char *words; static int i=0; int j; words=(char *) malloc (sizeof (char)); for (j=0; *(key+i) != NULL ; i++,j++ ) { *(words+j)=*(key+i); if(*(key+i) == ' ') { *(words+j)='\0'; i++; return words; } } *(words+j)='\0'; return words; } /* get document list for the given word */ Document* getDocumentList(char *word) { WordList *wl; wl=lookup(word,wordTable); if(wl!=NULL) return wl->doc; } /* comparisons of positions for successful search */ void findWordPosition(Position* fposList,Position *tposList) { Position *firstList,*secondList; long pos; for (firstList=fposList ; firstList != NULL ; firstList=firstList->next) { secondList=tposList; pos=firstList->pos; for ( ; firstList->pos <= secondList->pos ; secondList=secondList->next) { pos++; if ( pos == secondList->pos ) { firstList->flag=1; } } } } /* process documents for position of the given input terms */ void processDocumentList(Document *fdocList,char *word) { Document *ndocList,*tdocList; Position *nposList,*tposList; long *keyposList; tdocList=fdocList; ndocList=getDocumentList(word,wordTable); for(; ndocList->next != NULL ;ndocList=ndocList->next) { if (wcscmp(tdocList->docname,ndocList->docname) == 0) { tposList=tdocList->wordposition; nposList=ndocList->wordposition; findWordPosition(tposList,nposList); } } } /* print all the successful documents */ void printDocumentList(Document *docList) { Document *tempdoc; Position *posList; tempdoc=docList; for (; tempdoc != NULL ; tempdoc=tempdoc->next) { posList=docList->wordposition; for (; posList!=NULL ; posList=posList->next) { if (posList->flag==1) printf("\n\n%S Pos : %d\n\n",docList->docname,posList->pos); } } } /* perform search based on key term(s) given */ void doSearch(char *key) { Document *fdocList,*tempdocList; int i,j=0,k=0,wcount=0,doccount=0,pcount=0; char *word; word=key; while (!*word) { if(*word==' ') wcount++; } wcount++; word=getNextWord(key); fdocList=getDocumentList(word,wordTable); tempdocList=fdocList; if ( tempdocList == NULL ) { printf("No such term in any document\n"); return ; } for (; tempdocList->next != NULL ; tempdocList=tempdocList->next ) { for ( i=1; i<wcount; i++ ) { word=getNextWord(key); processDocumentList(tempdocList,word,wordTable); } } printDocumentList(fdocList); } /* free hash table entries */ void cleanup(){ int i; WordList *wl,*temp; for (i=0; i<HASHSIZE; i++) { if(wordTable[i]!=NULL){ wl=wordTable[i]; while(wl!=NULL){ temp=wl->next; free(wl); wl=temp; } } } }
•
•
Join Date: Apr 2008
Posts: 118
Reputation:
Solved Threads: 12
-1
#7 Oct 16th, 2009
•
•
•
•
If you're going to call the code 'wrong', do you mind giving an example?
To be honest Marco, your posts are getting a bit boring. You always post something like: 'This sux, use win32 API' and sometimes you add a link to somesort of crappy newsgroup.
Don't call something wrong if you can't explain why.
Newsgroups (Usenet) is by far the best place on the Web, for more than 20 years !
There are the greatest Win32 gurus, MS consultants, some of Windows creators, and best-sellers authors in the world (you can even find Bjarne Stroustrup himself...)
Aaaaaaah... those kids who know nothing...
•
•
Join Date: Apr 2009
Posts: 6
Reputation:
Solved Threads: 0
-1
#8 Oct 16th, 2009
•
•
•
•
Newsgroups (Usenet) is by far the best place on the Web, for more than 20 years !
There are the greatest Win32 gurus, MS consultants, some of Windows creators, and best-sellers authors in the world (you can even find Bjarne Stroustrup himself...)
+ 1000 !
(and Charles Petzold, but this newbie certainly even don't know him...)
•
•
Join Date: Mar 2008
Posts: 1,407
Reputation:
Solved Threads: 114
0
#9 Oct 17th, 2009
•
•
•
•
You're a complete noob. (Probably very , very young.. )
Newsgroups (Usenet) is by far the best place on the Web, for more than 20 years !
There are the greatest Win32 gurus, MS consultants, some of Windows creators, and best-sellers authors in the world (you can even find Bjarne Stroustrup himself...)
Aaaaaaah... those kids who know nothing...
•
•
•
•
Aaaaaaah... those kids who know nothing...
I need pageviews! most fun profile ever :)
•
•
Join Date: Oct 2009
Posts: 4
Reputation:
Solved Threads: 0
/* SearchEngine.cpp : Creating Search Index and perform searching with the given
input term(S) */
#include <stdio.h>
#include <windows.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#include <limits.h>
#define HASHSIZE 101
typedef struct _Document Document;
typedef struct _WordList WordList;
typedef struct _Position Position;
/* Hold positions of words in file */
struct _Position {
long pos;
int flag;
Position *next;
};
/* Hold list of Documents */
struct _Document {
TCHAR *docname;
int count;
Position *wordposition;
Document *next;
};
/* Hold list of words */
struct _WordList {
char *name;
WordList *next;
Document *doc;
};
int main(int argc, char * argv[]) {
char *dirName;
char key[256];
WordList* wordTable[HASHSIZE];
void performDirectoryScan(char *dirName,WordList* wordTable[]);
void createSearchIndex(WordList* wordTable[],char *dirName,char *fileName,HANDLE h);
void initWordTable(WordList* wordTable[]);
unsigned int hash(char *word);
WordList* lookup(WordList* wordTable[],char *word);
char* getStringDuplicate(char *words);
char* getNextWord(char *key);
WordList* getNewWordList(char *words);
Document* getNewDocument(char *fileName,long wordpos);
Document* getDocumentList(char *word);
void insertToWordTable(WordList* wordTable[],char **words,long wordpos,char *fileName);
void insertWordPosition(Document* docName,long wordpos);
void findWordPosition(Position* fposList,Position *tposList);
void processDocumentList(Document *fdocList,char *word);
void printDocumentList(Document *docList);
void doSearch(WordList* wordTable[],char *key);
void displayTable(WordList* wordTable[]);
void cleanup(WordList* wordTable[]);
dirName=(char *)malloc((strlen(argv[1])+ 1)* sizeof(char));
strcpy(dirName,argv[1]);
initWordTable(&wordTable);
performDirectoryScan(dirName,wordTable);
displayTable(wordTable);
printf("\n Enter Search term :");
scanf("%s",key);
doSearch(wordTable,key);
cleanup(wordTable);
getch();
return 0;
}
/* Initialize the word table */
void initWordTable(WordList* wordTable[]) {
int i;
for(i=0;i<HASHSIZE;i++) {
wordTable[i]=NULL;
}
}
/* return hash value */
unsigned int hash(char words[]){
unsigned int h=0;
int i;
for( i=0 ; words[i] != NULL ; i++)
h=words[i]+h*31;
return h%HASHSIZE;
}
/* return duplicate string */
char* getStringDuplicate(char *words){
int l=strlen(words)+1;
char *dupStr=(char*)malloc(l*sizeof (char) );
strcpy(dupStr,words);
if(dupStr==NULL)
return NULL;
else
return dupStr;
}
/* Scanning all files from Directory */
void performDirectoryScan(char *dirName,WordList* wordTable[])
{
WIN32_FIND_DATA info;
HANDLE h;
char *dirPath;
int i=0;
dirPath=(char *)malloc((strlen(dirName)+4)*sizeof (char));
strcpy(dirPath,dirName);
strcat(dirPath,"*.*");
h = FindFirstFile(dirPath,&info);
if (h == INVALID_HANDLE_VALUE)
{
return;
}
do
{
if (strcmp(info.cFileName, ".") != 0 && strcmp(info.cFileName, "..") != 0)
{
createSearchIndex(wordTable,dirName,info.cFileName,h);
}
} while (FindNextFile(h, &info));
FindClose(h);
}
/* Creating Indexer using words scanned */
void createSearchIndex(WordList* wordTable[],char *dirName,TCHAR *fileName,HANDLE h) {
FILE *inputFile;
char ch,*words;
int i=0,j=0;
int length=0;
long wordpos;
words=(char *)malloc(1000 * sizeof (char));
strcat(dirName,fileName);
inputFile = fopen(dirName,"r");
if (inputFile==NULL) {
printf("File does not exist");
return;
}
ch = fgetc (inputFile);
while (ch != EOF) {
words[i++] = ch;
wordpos=ftell(inputFile);
if(ch == ' ') {
words[--i]='\0';
wordpos=(wordpos-strlen(words)) + 1;
insertToWordTable(wordTable,words,wordpos,fileName);
i=0;
}
ch = fgetc(inputFile);
}
words[--i]='\0';
insertToWordTable(wordTable,words,wordpos,fileName);
free(words);
fclose (inputFile);
}
/* create a new world list */
WordList* getNewWordList(char *words) {
char *name;
Document *newdoc;
WordList *wl;
Position *newpos;
newpos=(Position *)malloc(sizeof (Position));
newdoc=(Document *)malloc(sizeof (Document));
wl=(WordList*)malloc(sizeof (WordList));
newpos->pos=0;
newpos->flag=0;
newpos->next=NULL;
newdoc->count=0;
newdoc->docname=(TCHAR *)malloc(sizeof(TCHAR));
newdoc->wordposition=newpos;
newdoc->next=NULL;
wl->name=getStringDuplicate(words);
wl->doc=newdoc;
wl->next=newdoc;
return wl;
}
/* Look up routine to scan words that are hashed */
WordList* lookup(char *words,WordList* wordTable[]){
unsigned int hi=hash(words);
WordList* wl;
wl=wordTable[hi];
for ( ; wl!=NULL ; wl=wl->next ) {
if(!strcmp(wl->name,words))
return wl;
}
wl=getNewWordList(words);
wl->next=wordTable[hi];
wordTable[hi]=wl;
return wl;
}
/* Returns new position of given word */
void insertWordPosition(Document* docName,long wordpos) {
Position *curpos,*newpos;
newpos=(Position *)malloc(sizeof(Position));
if (newpos != NULL) {
newpos->pos=wordpos;
newpos->flag=0;
newpos->next=NULL;
}
curpos=docName->wordposition;
while(curpos->next != NULL) {
curpos=curpos->next;
}
curpos->next=newpos;
}
/* returns a new document */
Document* getNewDocument(TCHAR *fileName,long wordpos) {
char *str;
Document *newdoc;
Position *newpos;
newdoc=(Document *)malloc(sizeof (Document) );
newpos=(Position *)malloc(sizeof (Position) );
newdoc->count=0;
newdoc->docname=(TCHAR *)malloc(sizeof (TCHAR));
wcscpy(newdoc->docname,fileName);
newdoc->count++;
newpos->pos=wordpos;
newpos->flag=0;
newpos->next=NULL;
newdoc->wordposition=newpos;
newdoc->next=NULL;
return newdoc;
}
/* Insert the term into hashtable based on hashvalue */
void insertToWordTable(WordList* wordTable[],char *words,long wordpos,TCHAR *fileName){
unsigned int hi;
WordList* wl;
Position *temppos,*newpos;
Document *tempdoc,*newdoc,*prevdoc,*curdoc;
wl=lookup(words,wordTable);
curdoc=wl->doc;
prevdoc=curdoc;
while ( curdoc != NULL ) {
if ( wcscmp(curdoc->docname,fileName)==NULL ){
curdoc->count++;
insertWordPosition(curdoc,wordpos);
return;
}
prevdoc=curdoc;
curdoc=curdoc->next;
}
newdoc=getNewDocument(fileName,wordpos);
if ( wl->doc==NULL ){
wl->doc=newdoc;
return;
}
prevdoc->next=newdoc;
return;
}
/* Display Contents of the table */
void displayTable(WordList* wordTable[]){
int i;
WordList* wl;
Document *tempdoc;
printf("\nWords In the Hash Table are:\n");
for ( i=0 ; i<HASHSIZE ; i++){
wl=wordTable[i];
for ( ; wl!=NULL ; wl=wl->next ) {
printf("\n \nWord :%s \n",wl->name);
for ( tempdoc=wl->doc ; tempdoc!=NULL ; tempdoc=tempdoc->next ) {
printf("\nDocument name is: %S",tempdoc->docname);
printf("\n Count : %d",tempdoc->count);
}
}
}
}
/* get all the words from input key */
char* getNextWord(char *key) {
char *words;
static int i=0;
int j;
words=(char *) malloc (sizeof (char));
for (j=0; *(key+i) != NULL ; i++,j++ ) {
*(words+j)=*(key+i);
if(*(key+i) == ' ') {
*(words+j)='\0';
i++;
return words;
}
}
*(words+j)='\0';
return words;
}
/* get document list for the given word */
Document* getDocumentList(WordList* wordTable[],char *word) {
WordList *wl;
wl=lookup(word,wordTable);
if(wl!=NULL)
return wl->doc;
}
/* comparisons of positions for successful search */
void findWordPosition(Position* fposList,Position *tposList) {
Position *firstList,*secondList;
long pos;
for (firstList=fposList ; firstList != NULL ; firstList=firstList->next) {
secondList=tposList;
pos=firstList->pos;
for ( ; firstList->pos <= secondList->pos ; secondList=secondList->next) {
pos++;
if ( pos == secondList->pos ) {
firstList->flag=1;
}
}
}
}
/* process documents for position of the given input terms */
void processDocumentList(Document *fdocList,char *word,WordList wordTable[]) {
Document *ndocList,*tdocList;
Position *nposList,*tposList;
long *keyposList;
tdocList=fdocList;
ndocList=getDocumentList(wordTable,word);
for(; ndocList->next != NULL ;ndocList=ndocList->next) {
if (wcscmp(tdocList->docname,ndocList->docname) == 0) {
tposList=tdocList->wordposition;
nposList=ndocList->wordposition;
findWordPosition(tposList,nposList);
}
}
}
/* print all the successful documents */
void printDocumentList(Document *docList) {
Document *tempdoc;
Position *posList;
tempdoc=docList;
for (; tempdoc != NULL ; tempdoc=tempdoc->next) {
posList=docList->wordposition;
for (; posList!=NULL ; posList=posList->next) {
if (posList->flag==1)
printf("\n\n%S Pos : %d\n\n",docList->docname,posList->pos);
}
}
}
/* perform search based on key term(s) given */
void doSearch(WordList* wordTable[],char *key) {
Document *fdocList,*tempdocList;
int i,j=0,k=0,wcount=0,doccount=0,pcount=0;
char *word;
word=key;
while (!*word) {
if(*word==' ')
wcount++;
}
wcount++;
word=getNextWord(key);
fdocList=getDocumentList(wordTable,word);
tempdocList=fdocList;
if ( tempdocList == NULL ) {
printf("No such term in any document\n");
return ;
}
for (; tempdocList->next != NULL ; tempdocList=tempdocList->next ) {
for ( i=1; i<wcount; i++ ) {
word=getNextWord(key);
processDocumentList(tempdocList,word,wordTable);
}
}
printDocumentList(fdocList);
}
/* free hash table entries */
void cleanup(WordList* wordTable[]){
int i;
WordList *wl,*temp;
for (i=0; i<HASHSIZE; i++) {
if(wordTable[i]!=NULL){
wl=wordTable[i];
while(wl!=NULL){
temp=wl->next;
free(wl);
wl=temp;
}
}
}
}
input term(S) */
#include <stdio.h>
#include <windows.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#include <limits.h>
#define HASHSIZE 101
typedef struct _Document Document;
typedef struct _WordList WordList;
typedef struct _Position Position;
/* Hold positions of words in file */
struct _Position {
long pos;
int flag;
Position *next;
};
/* Hold list of Documents */
struct _Document {
TCHAR *docname;
int count;
Position *wordposition;
Document *next;
};
/* Hold list of words */
struct _WordList {
char *name;
WordList *next;
Document *doc;
};
int main(int argc, char * argv[]) {
char *dirName;
char key[256];
WordList* wordTable[HASHSIZE];
void performDirectoryScan(char *dirName,WordList* wordTable[]);
void createSearchIndex(WordList* wordTable[],char *dirName,char *fileName,HANDLE h);
void initWordTable(WordList* wordTable[]);
unsigned int hash(char *word);
WordList* lookup(WordList* wordTable[],char *word);
char* getStringDuplicate(char *words);
char* getNextWord(char *key);
WordList* getNewWordList(char *words);
Document* getNewDocument(char *fileName,long wordpos);
Document* getDocumentList(char *word);
void insertToWordTable(WordList* wordTable[],char **words,long wordpos,char *fileName);
void insertWordPosition(Document* docName,long wordpos);
void findWordPosition(Position* fposList,Position *tposList);
void processDocumentList(Document *fdocList,char *word);
void printDocumentList(Document *docList);
void doSearch(WordList* wordTable[],char *key);
void displayTable(WordList* wordTable[]);
void cleanup(WordList* wordTable[]);
dirName=(char *)malloc((strlen(argv[1])+ 1)* sizeof(char));
strcpy(dirName,argv[1]);
initWordTable(&wordTable);
performDirectoryScan(dirName,wordTable);
displayTable(wordTable);
printf("\n Enter Search term :");
scanf("%s",key);
doSearch(wordTable,key);
cleanup(wordTable);
getch();
return 0;
}
/* Initialize the word table */
void initWordTable(WordList* wordTable[]) {
int i;
for(i=0;i<HASHSIZE;i++) {
wordTable[i]=NULL;
}
}
/* return hash value */
unsigned int hash(char words[]){
unsigned int h=0;
int i;
for( i=0 ; words[i] != NULL ; i++)
h=words[i]+h*31;
return h%HASHSIZE;
}
/* return duplicate string */
char* getStringDuplicate(char *words){
int l=strlen(words)+1;
char *dupStr=(char*)malloc(l*sizeof (char) );
strcpy(dupStr,words);
if(dupStr==NULL)
return NULL;
else
return dupStr;
}
/* Scanning all files from Directory */
void performDirectoryScan(char *dirName,WordList* wordTable[])
{
WIN32_FIND_DATA info;
HANDLE h;
char *dirPath;
int i=0;
dirPath=(char *)malloc((strlen(dirName)+4)*sizeof (char));
strcpy(dirPath,dirName);
strcat(dirPath,"*.*");
h = FindFirstFile(dirPath,&info);
if (h == INVALID_HANDLE_VALUE)
{
return;
}
do
{
if (strcmp(info.cFileName, ".") != 0 && strcmp(info.cFileName, "..") != 0)
{
createSearchIndex(wordTable,dirName,info.cFileName,h);
}
} while (FindNextFile(h, &info));
FindClose(h);
}
/* Creating Indexer using words scanned */
void createSearchIndex(WordList* wordTable[],char *dirName,TCHAR *fileName,HANDLE h) {
FILE *inputFile;
char ch,*words;
int i=0,j=0;
int length=0;
long wordpos;
words=(char *)malloc(1000 * sizeof (char));
strcat(dirName,fileName);
inputFile = fopen(dirName,"r");
if (inputFile==NULL) {
printf("File does not exist");
return;
}
ch = fgetc (inputFile);
while (ch != EOF) {
words[i++] = ch;
wordpos=ftell(inputFile);
if(ch == ' ') {
words[--i]='\0';
wordpos=(wordpos-strlen(words)) + 1;
insertToWordTable(wordTable,words,wordpos,fileName);
i=0;
}
ch = fgetc(inputFile);
}
words[--i]='\0';
insertToWordTable(wordTable,words,wordpos,fileName);
free(words);
fclose (inputFile);
}
/* create a new world list */
WordList* getNewWordList(char *words) {
char *name;
Document *newdoc;
WordList *wl;
Position *newpos;
newpos=(Position *)malloc(sizeof (Position));
newdoc=(Document *)malloc(sizeof (Document));
wl=(WordList*)malloc(sizeof (WordList));
newpos->pos=0;
newpos->flag=0;
newpos->next=NULL;
newdoc->count=0;
newdoc->docname=(TCHAR *)malloc(sizeof(TCHAR));
newdoc->wordposition=newpos;
newdoc->next=NULL;
wl->name=getStringDuplicate(words);
wl->doc=newdoc;
wl->next=newdoc;
return wl;
}
/* Look up routine to scan words that are hashed */
WordList* lookup(char *words,WordList* wordTable[]){
unsigned int hi=hash(words);
WordList* wl;
wl=wordTable[hi];
for ( ; wl!=NULL ; wl=wl->next ) {
if(!strcmp(wl->name,words))
return wl;
}
wl=getNewWordList(words);
wl->next=wordTable[hi];
wordTable[hi]=wl;
return wl;
}
/* Returns new position of given word */
void insertWordPosition(Document* docName,long wordpos) {
Position *curpos,*newpos;
newpos=(Position *)malloc(sizeof(Position));
if (newpos != NULL) {
newpos->pos=wordpos;
newpos->flag=0;
newpos->next=NULL;
}
curpos=docName->wordposition;
while(curpos->next != NULL) {
curpos=curpos->next;
}
curpos->next=newpos;
}
/* returns a new document */
Document* getNewDocument(TCHAR *fileName,long wordpos) {
char *str;
Document *newdoc;
Position *newpos;
newdoc=(Document *)malloc(sizeof (Document) );
newpos=(Position *)malloc(sizeof (Position) );
newdoc->count=0;
newdoc->docname=(TCHAR *)malloc(sizeof (TCHAR));
wcscpy(newdoc->docname,fileName);
newdoc->count++;
newpos->pos=wordpos;
newpos->flag=0;
newpos->next=NULL;
newdoc->wordposition=newpos;
newdoc->next=NULL;
return newdoc;
}
/* Insert the term into hashtable based on hashvalue */
void insertToWordTable(WordList* wordTable[],char *words,long wordpos,TCHAR *fileName){
unsigned int hi;
WordList* wl;
Position *temppos,*newpos;
Document *tempdoc,*newdoc,*prevdoc,*curdoc;
wl=lookup(words,wordTable);
curdoc=wl->doc;
prevdoc=curdoc;
while ( curdoc != NULL ) {
if ( wcscmp(curdoc->docname,fileName)==NULL ){
curdoc->count++;
insertWordPosition(curdoc,wordpos);
return;
}
prevdoc=curdoc;
curdoc=curdoc->next;
}
newdoc=getNewDocument(fileName,wordpos);
if ( wl->doc==NULL ){
wl->doc=newdoc;
return;
}
prevdoc->next=newdoc;
return;
}
/* Display Contents of the table */
void displayTable(WordList* wordTable[]){
int i;
WordList* wl;
Document *tempdoc;
printf("\nWords In the Hash Table are:\n");
for ( i=0 ; i<HASHSIZE ; i++){
wl=wordTable[i];
for ( ; wl!=NULL ; wl=wl->next ) {
printf("\n \nWord :%s \n",wl->name);
for ( tempdoc=wl->doc ; tempdoc!=NULL ; tempdoc=tempdoc->next ) {
printf("\nDocument name is: %S",tempdoc->docname);
printf("\n Count : %d",tempdoc->count);
}
}
}
}
/* get all the words from input key */
char* getNextWord(char *key) {
char *words;
static int i=0;
int j;
words=(char *) malloc (sizeof (char));
for (j=0; *(key+i) != NULL ; i++,j++ ) {
*(words+j)=*(key+i);
if(*(key+i) == ' ') {
*(words+j)='\0';
i++;
return words;
}
}
*(words+j)='\0';
return words;
}
/* get document list for the given word */
Document* getDocumentList(WordList* wordTable[],char *word) {
WordList *wl;
wl=lookup(word,wordTable);
if(wl!=NULL)
return wl->doc;
}
/* comparisons of positions for successful search */
void findWordPosition(Position* fposList,Position *tposList) {
Position *firstList,*secondList;
long pos;
for (firstList=fposList ; firstList != NULL ; firstList=firstList->next) {
secondList=tposList;
pos=firstList->pos;
for ( ; firstList->pos <= secondList->pos ; secondList=secondList->next) {
pos++;
if ( pos == secondList->pos ) {
firstList->flag=1;
}
}
}
}
/* process documents for position of the given input terms */
void processDocumentList(Document *fdocList,char *word,WordList wordTable[]) {
Document *ndocList,*tdocList;
Position *nposList,*tposList;
long *keyposList;
tdocList=fdocList;
ndocList=getDocumentList(wordTable,word);
for(; ndocList->next != NULL ;ndocList=ndocList->next) {
if (wcscmp(tdocList->docname,ndocList->docname) == 0) {
tposList=tdocList->wordposition;
nposList=ndocList->wordposition;
findWordPosition(tposList,nposList);
}
}
}
/* print all the successful documents */
void printDocumentList(Document *docList) {
Document *tempdoc;
Position *posList;
tempdoc=docList;
for (; tempdoc != NULL ; tempdoc=tempdoc->next) {
posList=docList->wordposition;
for (; posList!=NULL ; posList=posList->next) {
if (posList->flag==1)
printf("\n\n%S Pos : %d\n\n",docList->docname,posList->pos);
}
}
}
/* perform search based on key term(s) given */
void doSearch(WordList* wordTable[],char *key) {
Document *fdocList,*tempdocList;
int i,j=0,k=0,wcount=0,doccount=0,pcount=0;
char *word;
word=key;
while (!*word) {
if(*word==' ')
wcount++;
}
wcount++;
word=getNextWord(key);
fdocList=getDocumentList(wordTable,word);
tempdocList=fdocList;
if ( tempdocList == NULL ) {
printf("No such term in any document\n");
return ;
}
for (; tempdocList->next != NULL ; tempdocList=tempdocList->next ) {
for ( i=1; i<wcount; i++ ) {
word=getNextWord(key);
processDocumentList(tempdocList,word,wordTable);
}
}
printDocumentList(fdocList);
}
/* free hash table entries */
void cleanup(WordList* wordTable[]){
int i;
WordList *wl,*temp;
for (i=0; i<HASHSIZE; i++) {
if(wordTable[i]!=NULL){
wl=wordTable[i];
while(wl!=NULL){
temp=wl->next;
free(wl);
wl=temp;
}
}
}
}
![]() |
Similar Threads
- In VB6.0 How do i want to find function get path windows fonts directory? (Visual Basic 4 / 5 / 6)
- Counting number of files in a directory (Java)
- How to use find to handle large number of files (Getting Started and Choosing a Distro)
- Help: applet can not find image files (Java)
- function not declared error (C++)
- plz help me in solving this files concept question (Java)
- How to count folder files using php (PHP)
- To open and close in turn all the files in a directory (C++)
Other Threads in the C Forum
- Previous Thread: last error in linux
- Next Thread: Addicon problem
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






