Esempio n. 1
0
// Read a file and obtain words and positions of the words and save them in table.
int wtable_createFromFile(WordTable * wtable, char * fileName, int verbose)
{
	// Write your code here
    FILE *fp;
    fp = fopen(fileName, "r");

    //LinkedList * positions;
    //int positions = 0;
    char * word;
    char c;

    int position;

    int garbage;
    int length;
    count = 0;
    if (fp) {
        while (word != NULL) {
            //c = fgetc(fp);
            //fscanf(fp, "%s", word);
                //positions = wtable_getPositions(wtable, word);
            //if (isalpha(c)) {
                word = nextword(fp);
                toLower(word);
                //LinkedList * positions = wtable_getPositions(wtable, word);
                //position = list->head;
                if (word != NULL) {
                    length = strlen(word);
                    wtable_add(wtable, word, wordPos - length - 1);
                    //wtable->nWords++;
                    if (verbose == 1)
                        printf("%d: word=%s, pos=%d\n", count - 1, word, wordPos - length - 1);
                    //count = 0;
                }
                //positions++;
           //}

        } //while (word != NULL); //while (c != EOF);
    }
    //count = 0;
    return 0;
}
// Read a file and obtain words and positions of the words and save them in table.
int wtable_createFromFile(WordTable * wtable, char * fileName, int verbose)
{
	// Write your code here
	FILE *fd = fopen(fileName, "r");
	if (fd == NULL) {return 0;}
	
	char *w;
	wordPos = 0;
	charCount = 0;

	while ((w = nextword(fd)) != NULL) {
	     wordCount++;
	     toLower(w);
	     wordPos = charCount - wordLength - 1;
	     if (verbose == 1) {
	         printf("%d: word=%s, pos=%d\n", wordCount - 1, w, wordPos);
	     }
	     wtable_add(wtable, word, wordPos);
	}
	return 0;
}