Month

Brief Information about the June 99 CSIG Meeting

Find OR--- A Utility for processing text files.
Sample Output

Author: Bruce Arnold


Welcome to the C++ Users Group for June 1999.

In this month's main presentation we will discuss a program that I've been using for several years now to help me analyze network log files that are produced with backup tapes. Typical log files can be hundreds of lines long. To check whether a tape backup was successful it is often only required to go to the end of the log file and look for a success or failure. If there's a failure, then back track through the log file looking for errors.

This procedure seems simple enough but when you have five to ten different log files to check every morning it becomes tedious. Using the FIND command that comes with DOS, Windows, Windows 95, etc. batch files can be created to filter out only the important lines of these log files. Except: The one option that I really needed was not in the FIND command so I created a program to accomplish what I needed.

Tonight's program, called FINDOR, allows searching for lines in a text file that contain up to 99 keywords that are logically "OR"ed together. In the case of the network log files, I look for the lines that contain the keywords ERROR or STATUS or BYTES SAVED or TOTAL BYTES. The resulting display is only a few lines long and provides me with all the information that I need. I also found this program quite useful in analyzing other forms of log files.

The program is written in the basic "C" language and demonstrates a number of concepts. These include

  1. parsing the command line
  2. reading a text file line by line
  3. searching for strings
  4. obtaining time and date values about a file
  5. sorting items on a command line
  6. special formatting for a Laser Jet printer
  7. wildcard filename processing
The program runs from a command line and should compile with most 16 and 32 bit compilers.

void process(char *filename, char *token[])
    {
    char linebuff[MAXWIDTH];	FILE *fs;  int i;
    if (NULL==(fs=fopen(filename,"r"))) 
	{ perror(filename); ++diskError; return; }

    i = fread(linebuff,1, MAXWIDTH, fs);
    if (is_binary(linebuff,i))	{ fclose(fs);  return;	}
    rewind(fs);
    
    if      (laserFlag ) Laserformat(strlwr(filename));
    else if (laser2Flag) Laser2format(strupr(filename));
    else printf("  [ File:  %s ]\n",strupr(filename));

// -----------------------------------------------------------------
    printf("  Lines containing:\n");

    printf("\t\t     %s\n",token[0]);
    for(i=1; NULL!=token[i]; i++)
    	printf("\t\t  or %s\n",token[i]);
    printf("\n");
// -----------------------------------------------------------------

    while (!feof(fs))
	{
	if (NULL==fgets(linebuff, MAXWIDTH, fs)) break;
	for(i=0; token[i]; i++)
	    {
	    if (strstri(linebuff,token[i]))
		{
		fputs(linebuff,stdout);
		break;
		}
	    }
	}
    fclose(fs); fputs("\f",stdout);
    }


"Random Access" questions start at 7:30 Tuesday night.

SOURCE CODE

Source Code Files

For help, email me at b a r n o l d @ b l a s t . n e t
Back to C++ Main Page