int main(int argc,char* argv[]) { Hashtable* theTable = readWordList("pairs.txt"); playGame(theTable); destroyHTable(theTable); return 0; }
/** Start of program. Accepts input from console with name of files to read and name of files to write. Checks files for reading/writing. Then, calls on other methods to read through the file and uncompress the file. @param argc - number of commands user submitted @param argv - string text for each command from user @return Sucessfull end of the program */ int main( int argc, char *argv[] ) { //Default file which contains the "dictionary" of words char *wordFile = "words.txt"; //Open file from agrs FILE *input; FILE *output; //Check to make sure user entered the correct number of arguments if ( argc < ( ARGS - 1 ) || argc > ARGS ){ fprintf(stderr, "usage: unpack <compressed.raw> <output.txt> [word_file.txt]\n" ); exit( EXIT_FAILURE ); } //Optional input. If user enters optional file name, program will use it if ( argc == ARGS ) { wordFile = argv[ ARGS - 1 ]; } //Create wordList for our "dictionary" WordList *wordList = readWordList( wordFile ); //Open file for reading. Procduces error if file cannot open. if ( ( input = fopen( argv[ 1 ], "r" ) ) == NULL ){ fprintf(stderr, "Can't open file: %s\n", argv[ 1 ] ); fprintf(stderr, "usage: unpack <compressed.raw> <output.txt> [word_file.txt]\n" ); exit( EXIT_FAILURE ); //Open file for writing. Produces error if file cannot open. } else if ( ( output = fopen( argv[ 2 ], "w" ) ) == NULL ){ fprintf(stderr, "Can't open file: %s\n", argv[ 2 ] ); fprintf(stderr, "usage: unpack <compressed.raw> <output.txt> [word_file.txt]\n" ); exit( EXIT_FAILURE ); } //Variable to hold leftover binary code PendingBits pending = { 0, 0 }; //Loop that runs through file, writting letters to the writer file int number; //Continues to call method until method returns -1 while ( (number = readCode( &pending, input ) ) != -1 ){ //Prints words unless number is -1 or -2 if ( number != -1 && number != -2 ){ fprintf(output, "%s", wordList->words[ number ] ); } } //free wordList freeWordList( wordList ); //Close file fclose( input ); fclose( output ); return EXIT_SUCCESS; }
void build(std::istream &in, MutableHypergraph<Arc> *outHg, FinishBuilding &finish) const { IVocabularyPtr vocab = outHg->getVocabulary(); typedef typename Arc::Weight Weight; WeightedStrings<Weight> ws(vocab ? vocab : Vocabulary::createDefaultVocab()); readWordList(in, ws, wordlist, stringUnion.unweighted); outHg->clear(kFsm|kStoreFirstTailOutArcs|kCanonicalLex); BuildStringUnion<Arc> build(ws, *outHg, stringUnion); //sets vocab finish(build); }
/** Opens appropriate files and handles them @param argc number of command line arguments @param argv array of command line arguments @return the exit status of the program */ int main( int argc, char *argv[] ) { char *wordFile = "words.txt"; FILE *input; FILE *output; if ( argc < 3 || argc > 4 ) { fprintf( stderr, "usage: pack <input.txt> <compressed.raw> [wordfile.txt]\n" ); exit( 1 ); } input = fopen( argv[ 1 ], "rb" ); if ( !input ) { printf( "usage: readFile <input-file>\n" ); exit( 1 ); } output = fopen( argv[ 2 ], "rw" ); if ( !output ) { printf( "usage: readFile <input-file>\n" ); exit( 1 ); } if ( argc == 4 ) { wordFile = argv[ 3 ]; } WordList *wordList = readWordList( wordFile ); #ifdef DEBUG // Report the entire contents of the word list, once it's built. printf( "---- word list -----\n" ); for ( int i = 0; i < wordList->len; i++ ) printf( "%d == %s\n", i, wordList->words[ i ] ); printf( "--------------------\n" ); #endif // ... // Read the contents of the whole file into one big buffer. This could be more // efficient, but it simplifies the rest of the program. char *buffer = readFile( input ); // Write out codes for everything in the buffer. int pos = 0; PendingBits pending = { 0, 0 }; while ( buffer[ pos ] ) { // Get the next code. int code = bestCode( wordList, buffer + pos ); #ifdef DEBUG printf( "%d <- %s\n", code, wordList->words[ code ] ); #endif // Write it out and move ahead by the number of characters we just encoded. readCode( &pending, input ); pos += strlen( wordList->words[ code ] ); } // Write out any remaining bits in the last, partial byte. flushBits( &pending, output ); // ... fclose( input ); fclose( output ); return EXIT_SUCCESS; }