Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <math.h>
- #include <ctype.h>
- char * searchBuffer( char *, char * , int * , int *, int);
- //
- //Above - Header file
- //Below - Main Source
- //
- #include "newgrep.h"
- int main ( int argc, char * argv[] ){
- char outFile [81], // Output file
- keyword [81], // Word for string compare
- filename [81], // File argument in argv(a.k.a. file)
- **lineOut, // Output from token loop
- *buffer, // Line for searching
- option; // Option for switch statement
- int i = 0, // Loop Counter
- j = 0, // Argument String elements
- k = 0, // Secondary Loop Counter
- l = 0, // lineOut element counter for searchBuffer return
- lines = 0, // how many lines occur is counted in
- occur = 0, // how many times keyword occurs
- boolI = 0, // Boolean value for option I
- boolO = 0; // Boolean value for option O
- FILE * fp, // File Pointer for input file (3rd arg)
- * ofp; // File pointer for output file
- //Set both character arrays to zero for option check
- filename[0] = 0;
- keyword[0] = 0;
- // Echo number of arguments passed into main
- if( argc < 3 ){
- fprintf (stderr, "\nInsufficient arguments!\n\n" );
- exit(1);
- }else{
- printf ( "\nThere were %d arguments passed to args.\n\n", argc);
- }
- // Finds Option elements, loops skips first argument(program name)
- for( i = 1 ; i < argc ; i++){ // Loop thru arguments, re-init i
- if ( argv[i][0] == '-' ){ // Look for '-' as first character
- j = strlen(argv[i]); // Set j for max loop cycles
- for( k = 0 ; k < j ; k++){ // loop cycles element i's char array
- option = argv[i][k]; // set current char to option
- switch( option ){ // test for either 'i'|'I' or 'o'|'O'
- case 'i':
- case 'I':
- boolI = 1;
- break;
- case 'o':
- case 'O':
- boolO = 1;
- if( argv[i+1][0] != NULL ){
- strcpy(outFile, argv[i+1]);
- }else{
- fprintf ( stderr, "Invalid File Argument <%s>!\n",
- argv[i+1][0]);
- exit(1);
- }
- break;
- default:
- break;
- }
- }
- }else if( keyword[0] == 0 ){
- strcpy(keyword, argv[i]);
- }else if( filename[0] == 0 ){
- strcpy(filename, argv[i]);
- }
- }
- // #########################
- // #### File Operations ####
- // #########################
- // Check to see if file is valid
- if ( (fp=fopen (filename, "r") ) == NULL){
- fprintf ( stderr, "File IO: Unable to open %s!\n",
- filename);
- exit(1);
- }
- // Allocate memory to buffer then read in until newline
- buffer = (char*) malloc(256);
- while ( fgets(buffer, 255, fp) != NULL ){
- if( buffer[strlen(buffer) - 1] == '\n' )
- buffer[strlen(buffer) - 1] = '\0'; // Replace new line at end
- lineOut[l] = searchBuffer( keyword, buffer, &lines, &occur, boolI);
- puts("searchBuffer WORKED!!!!");
- getchar();
- fflush(stdin);
- l++;
- }
- printf("well that wasn't so bad\n");
- //
- //Output section removed
- //
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement