//----------------------------------------------------------------------------- // FileIO.c // Finds the unique tokens contained in infile. Tokens are defined as the // maximal substrings of a line not containing any character in delim[]. // Unique tokens are printed to outfile in the order of first appearance in // infile. Upper and lower case chars are not distinguished. To distinguish // upper from lower, comment out the call to function lower() // // compile: gcc -std=c17 -Wall -o FileIO FileIO.c // // run: FileIO file1 file2 // // This program illustrates File input-output operations in C like fgets() // and fprintf(). It also illustrates string handling functions in C, such // as strlen(), strcmp(), strcat() and strtok(), all in string.h, and also // heap memory operations such as malloc(), calloc(), realloc() and free() // in stdlib.h. //----------------------------------------------------------------------------- #include #include #include #include #include // findWord() // Returns true if char array buf contains word, false otherwise. Array buf // must consist of consecutive strings separated by the null '\0' character. bool find(char* word, char* buf, int n){ char* next = buf; int i; for( i=0; i/?;:[{]}|`~!@#$%^&*()-_=+0123456789"; // check command line for correct number of arguments if( argc != 3 ){ fprintf(stderr, "Usage: %s \n", argv[0]); exit(EXIT_FAILURE); } // open files for reading and writing infile = fopen(argv[1], "r"); outfile = fopen(argv[2], "w"); if( infile==NULL ){ fprintf(stderr, "Unable to open file %s for reading\n", argv[1]); exit(EXIT_FAILURE); } if( outfile==NULL ){ fprintf(stderr, "Unable to open file %s for writing\n", argv[2]); exit(EXIT_FAILURE); } // read each line of infile, parse tokens, add unique tokens to tokenBuffer tokenCount = 0; tokenBufferSize = tokenBufferSizeIncrement; tokenBuffer = calloc(tokenBufferSize, sizeof(char)); tokenBufferNext = 0; while( fgets(lineBuffer, lineBufferSize, infile) != NULL ) { // get first token in lineBuffer token = strtok(lineBuffer, delim); while( token!=NULL ){ // a token exists // check if new lower(token); // don't distinguish upper and lower case if( find(token, tokenBuffer, tokenCount) ){ // already have it token = strtok(NULL, delim); // try another continue; } // expand tokenBuffer if necessary if( strlen(token)+2 > tokenBufferSize-tokenBufferNext ){ // #bytes needed > #bytes available tokenBufferSize += tokenBufferSizeIncrement; tokenBuffer = realloc(tokenBuffer, tokenBufferSize*sizeof(char)); } // append token to tokenBuffer strcat(&tokenBuffer[tokenBufferNext], token); tokenBufferNext += strlen(token)+1; tokenBuffer[tokenBufferNext] = '\0'; tokenCount++; // get next token in lineBuffer token = strtok(NULL, delim); } } // print tokens to outfile token = tokenBuffer; for( i=0; i