/* Dictionary program to demonstrate string file operations. */ /* */ /* Created for use in Computer Science 101 at USC. */ /* Copyright 1999 by Rick Wagner, all rights reserved. */ #include #include /* For tolower(). */ typedef struct { char sSpelling[10]; /* May not initialize struct in definition. */ int iNumMeanings; char sPartOfSpeech[3][10]; char sMeaning[3][30]; } Word; /* Function prototypes: */ void about(); /* Tell about the program. */ int loadDict(Word* d); /* Load the dictionary from disk file. */ void saveDict(Word* d, int n); /* Save the dictionary to disk file. */ void menu(Word* d, int* n); /* Menu driven user interface. */ void addWords(Word* d, int* n); /* Let the user add words to the dictionary. */ Word getWord(); /* Get a word definition from the user. */ void showDict(Word* d, int n); void main() { int iNumWords = 0; /* Can change during execution, pass by ref. */ Word Dictionary[1000]; /* Dictionary can hold 1000 words. */ iNumWords = loadDict(Dictionary); /* Find out how many words it holds by */ about(); /* loading it from the disk and counting. */ menu(Dictionary, &iNumWords); /* Pass both by reference. */ } void about() { puts(" Dictionary program to demonstrate string file operations."); puts(""); puts(" Created for use in Computer Science 101 at USC."); puts(" Copyright 1999 by Rick Wagner, all rights reserved."); puts(""); } /* Load the entire dictionary from the disk into memory. */ int loadDict(Word* d) { FILE* ptrInFile; /* FILE's a struct defined in stdio.h. */ int iNumItemsRead = 1; /* Initialized to make the while() go. */ int iNumWords = 0; /* Return value default is zero words. */ ptrInFile = fopen("dict.txt", "rb"); /* Open the file for "read, binary." */ if (ptrInFile != NULL) /* If the file doesn't exist, do */ { /* nothing. */ while (iNumItemsRead == 1) { iNumItemsRead = fread(&(d[iNumWords]), sizeof(d[0]), 1, ptrInFile); if (iNumItemsRead == 1) { iNumWords++; /* Increment the word count. */ } } fclose(ptrInFile); /* Close the input file. */ } return iNumWords; } /* Save the dictionary in memory to the disk. */ void saveDict(Word* d, int n) { FILE* ptrOutFile; int i = 0; /* Store the words in the dictionary file */ ptrOutFile = fopen("dict.txt", "wb"); /* Open the file for "write, binary." */ if (ptrOutFile != NULL) { for (i = 0; i < n; i++) { fwrite(&(d[i]), sizeof(d[i]), 1, ptrOutFile); } fclose(ptrOutFile); /* Close the output file. */ } else { puts("Null file pointer in saveDict()."); } } /* Menu driven user interface. */ void menu(Word* d, int* n) { char cChoice = 0; puts("a. Add words to the dictionary."); puts("b. Show the dictionary."); puts("c. Sort the dictionary."); puts("d. Search the dictionary."); puts(""); puts("q. Quit."); puts(""); printf("Enter your choice: "); scanf("%c", &cChoice); fflush(stdin); cChoice = (char) tolower((int) cChoice); switch (cChoice) { case 'a': { addWords(d, n); menu(d, n); break; } case 'b': { showDict(d, *n); menu(d, n); break; } case 'c': { puts("\nThis feature is not yet implemented."); menu(d, n); break; } case 'd': { puts("\nThis feature is not yet implemented."); menu(d, n); break; } case 'q': { puts("\nQuitting. Goodbye."); saveDict(d, *n); puts(" Dictionary saved."); break; } default: { puts("Bad choice, try again. "); menu(d, n); break; } } } /* Get a single word from the user. */ Word getWord() { int i = 0; Word InputWord; /* Initialize the Word to all null: */ InputWord.sSpelling[0] = '\0'; InputWord.iNumMeanings = 0; InputWord.sPartOfSpeech[0][0] = '\0'; InputWord.sPartOfSpeech[1][0] = '\0'; InputWord.sPartOfSpeech[2][0] = '\0'; InputWord.sMeaning[0][0] = '\0'; InputWord.sMeaning[1][0] = '\0'; InputWord.sMeaning[2][0] = '\0'; printf("Enter the new word: "); gets(InputWord.sSpelling); printf("How many meanings does this word have? "); scanf("%d", &InputWord.iNumMeanings); fflush(stdin); for (i = 0; i < InputWord.iNumMeanings; i++) { printf("Enter the new word's part of speech %d: ", i + 1); gets(InputWord.sPartOfSpeech[i]); printf("Enter the new word's meaning %d: ", i + 1); gets(InputWord.sMeaning[i]); } return InputWord; } /* Add words to the dictionary. */ void addWords(Word* d, int* n) { Word InputWord; char cChoice = 'y'; while (cChoice == 'y') { InputWord = getWord(); printf("Word: %s, Part of speech one: %s, Meaning one: %s.\n", InputWord.sSpelling, InputWord.sPartOfSpeech[0], InputWord.sMeaning[0]); d[*n] = InputWord; (*n)++; printf("\nEnter another word? (y/n)"); scanf("%c", &cChoice); fflush(stdin); cChoice = (char) tolower((int) cChoice); } } /* Display the dictionary words on the screen. Does not scale. */ void showDict(Word* d, int n) { int i = 0; int j = 0; puts("\n # Word POS Meaning\n"); for (i = 0; i < n; i++) { printf("%2d %10s %10s %30s\n", i + 1, d[i].sSpelling, d[i].sPartOfSpeech[0], d[i].sMeaning[0]); for (j = 1; j < d[i].iNumMeanings; j++) { printf(" %10s %30s\n", d[i].sPartOfSpeech[j], d[i].sMeaning[j]); } } printf("\n"); }