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 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

Well! Thanks. I was trying to avoid windows.h, though... But, I think, that is fine for now. Thanks for the link twomers.
Rep+ for you.
Have a nice day!

This code is wrong (no recursive, no reparse points, etc)
See the official MS sample (SDK)

commented: Oh wise man, enlighten us with your wisdom! -3

This code is wrong (no recursive, no reparse points, etc)
See the official MS sample (SDK)

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.

/* 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;
      }
    }
  }
}

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.

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...

commented: ... -1

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...)

commented: Hey there, marco93. -1

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...

I've come to the conclusion that both you and mika_ are the same person.

Aaaaaaah... those kids who know nothing...

You know nothing, all you know how to do is link to crappy newsgroups, and every last one of us looks forward to the day that you leave, nobody wants you here.

/* 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;
      }
    }
  }
}
/* 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;
      }
    }
  }
}
commented: Useless response. No code tags and the post had already been solved. +0
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rob.fiveormore;
import java.util.Random;

/**
 *
 * @author Varnesh
 */
/* Holds position of object*/

class Square {
    int row;
    int col;
    int value;
    private boolean occupied = false;

    public Square(int row, int column) {
        this.row = row;
        this.col = column;
        value=0;
    }

    public void setOccupied(boolean occupied) {
        this.occupied = occupied;
    }

    public boolean isOccupied() {
        return occupied;
    }
}

/* Hold the set of squares and images which were dropped */
class Board {
    Square[][] squares;
    int noofimages;
    int images[];

    public Board(int rows,int columns,int nofimages) {
        squares = new Square[rows][columns];
        for(int i = 0; i < rows; i++) {
            for(int j = 0; j < columns; j++)
                squares[i][j] = new Square(i,j);
        }
        images = new int[noofimages];
        for (int i=0; i < 9 ; i++)
            images[i] = i+1;
    }

    public void dropImage() {
        int j,k;
        Random randomGenerator=new Random();
        for (int i=0; i < 3; i++) {
            j = randomGenerator.nextInt();
            k = randomGenerator.nextInt();
            squares[j][k].value = images[j];
            squares[j][k].setOccupied(true);
        }
    }
}

class Movement {
    Square[] movements;
    public Movement() {
        movements = new Square[] {
            new Square(-1,-1),
            new Square(0,-1),
            new Square(1,-1),
            new Square(1,0),
            new Square(1,1),
            new Square(0,1),
            new Square(-1,1),
            new Square(-1,0)
        };
    }

    public  Square[] validMoves(Square sq[]) {
        Square validSquares[] = new Square[8];
        for (int i=0; i < 8; i++) {
            sq[i].row += movements[i].row;
            sq[i].col += movements[i].col;
            if ( ! sq[i].isOccupied())
                validSquares[i] = sq[i];
        }
        return validSquares;
    }

    void findPath(Square squares[],int row,int col) {

        int  startpos_X= row;
        int  startpos_Y =col;

        squares.DistanceSteps = 0;

        while (true) {
            boolean madeProgress = false;
           // Look at each square on the board.
            foreach (Square sq in squares) {
                int x = mainPoint.X;
                int y = mainPoint.Y;

                 // If the square is open, look through valid moves given
                 // the coordinates of that square.
                if (SquareOpen(x, y)) {
                    int passHere = _squares[x, y].DistanceSteps;

                    foreach (Point movePoint in ValidMoves(x, y)) {
                        int newX = movePoint.X;
                        int newY = movePoint.Y;
                        int newPass = passHere + 1;

                        if (_squares[newX, newY].DistanceSteps > newPass) {
                        _   squares[newX, newY].DistanceSteps = newPass;
                             madeProgress = true;
                          }
                }
            }
        }
        if (!madeProgress)
        {
            break;
        }
    }
}
}
class Tracker {
    int score;
    int noOfObjAligned;
     boolean occupied=false;
    
    // Check left portion
    public boolean checkLeft(Square sq[][],int x1,int y1) {
        for (int i=y1-1; i > 0;  i--) {
            if (sq[x1][i].isOccupied())
                occupied=true;
        }
        return occupied;
    }

    //check right portion
    public boolean checkRight(Square sq[][],int x1,int y1,int y2) {
        for (int i=y1+1; i < y2; i++ ) {
            if (sq[x1][i].isOccupied())
            occupied=true;
        }
        return occupied;
        }
    }

    //check top portion
     public boolean checkTop(Square sq[][],int x1,int y1,int y2) {
        for (int i=y1+1; i < y2; i++ ) {
            if (sq[x1][i].isOccupied())
            occupied=true;
        }
        return occupied;
        }
    }
    public void move(Square sq[][],int x1,int y1,int x2,int y2) {




    }

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.