S-09 - pchBase = pchDictionary = (char *)malloc(ulLen);...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
/* Fatal -- print a message before expiring */ void Fatal(char *pchMsg, unsigned u) { fprintf(stdout, pchMsg, u); exit(1); } /* ReadDict -- read the dictionary file into memory and preprocess it * * A word of length cch in the dictionary is encoded as follows: * * byte 0 = cch + 3 * byte 1 = number of letters in the word * byte 2. .. = the word itself, null-terminated * * Observe that cch+3 is the length of the total encoding. These * byte streams are concatenated, and terminated with a 0. */ void ReadDict(char *pchFile) { FILE *fp; char * pch; char * pchBase; unsigned long ulLen; unsigned cWords = 0; unsigned cLetters; int ch; struct stat statBuf; if (stat(pchFile, &statBuf)) Fatal("Cannot stat dictionary\n", 0); ulLen = statBuf.st_size + 2 * (unsigned long)MAXWORDS;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: pchBase = pchDictionary = (char *)malloc(ulLen); if(pchDictionary == NULL) Fatal("Unable to allocate memory for dictionary\n", 0); if ((fp = fopen(pchFile, "r")) == NULL) Fatal("Cannot open dictionary\n", 0); while (!feof(fp)) { pch = pchBase+2; /* reserve for length */ cLetters = 0; while ((ch = fgetc(fp)) != '\n' && ch != EOF) { if (isalpha(ch)) cLetters++; *pch++ = ch; } *pch++ = '\0'; *pchBase = pch - pchBase; pchBase[1] = cLetters; pchBase = pch; cWords++; } fclose(fp); *pchBase++ = 0; fprintf(stdout, "main dictionary has %u entries\n", cWords); if (cWords >= MAXWORDS) Fatal("Dictionary too large; increase MAXWORDS\n", 0); fprintf(stdout, "%lu bytes wasted\n", ulLen - (pchBase - pchDictionary));...
View Full Document

This note was uploaded on 12/20/2009 for the course ECE 466 taught by Professor Staff during the Fall '09 term at Clarkson University .

Page1 / 2

S-09 - pchBase = pchDictionary = (char *)malloc(ulLen);...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online