You are here:

C/C Program BUG

Advertisement


Question
I have a C program that I need a loop bug fixing on.

Basically this is how the program works or should:-


I throw a file of information to this program that reads standard in,
(stdin)

I also pass an input variable to the program which tell the program
what type of string to match on, this being a test result value.

Below is an example of how I execute this command:-


type [myfilename] | [MYCPROGRAM] [ STRINGS TO MATCH on, eg. 1%r166 ]

The data from myfilename will look something like:-

Event<#>040708.135033
TESTRESULT<#>1%r166,-1.143775E+5,+5.865E+1,+4.845E+1,+5.1E+1,<#>All
TESTID<#>1%r166

Event<#>040708.135035
TESTRESULT<#>1%r166,-1.143775E+5,+5.865E+1,+4.845E+1,+5.1E+1,<#>All
TESTID<#>1%r166

Event<#>040708.135037
TESTRESULT<#>1%r166,-1.143775E+5,+5.865E+1,+4.845E+1,+5.1E+1,<#>All
TESTID<#>1%r169

Event<#>040709.135040
TESTRESULT<#>1%r166,-1.143775E+5,+5.865E+1,+4.845E+1,+5.1E+1,<#>All
TESTID<#>1%r166

Event<#>040708.135042
TESTRESULT<#>1%r166,-1.143775E+5,+5.865E+1,+4.845E+1,+5.1E+1,<#>All
TESTID<#>1%r188

Event<#>040710.135033
TESTRESULT<#>1%r166,-1.143775E+5,+5.865E+1,+4.845E+1,+5.1E+1,<#>All
TESTID<#>1%r166

etc ...

The C program first searches on Event, this is a date timestamp

I extract the date only, e.g yymmdd - 040708

I then extract the TESTRESULT and only match on it provided this is the
same as the input variable

The program then needs to count up how many of these r%166 values there
for a certain time, in other words I would like to sort a qty of these
values by a particular day, example:-

yymmdd

040708   - 1%r166 qty = 2
040709   - 1%r166 qty = 1
040710   - 1%r166 qty = 1

The C program creates a little HTML which it throws this info to standard out
but this is not really essential just sorting the data correctly is.

My **BUG** is that my counter resets somewhere and I get miscalulated
data, the test data from this file can be hard coded for testing just
so long as the program reports correct info and qty sorted by day.

Do you think you would be able to look at this and fix the problem, maybe
tell me where I'm going wrong.

Here is the source code:-

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>

#define RESET           0
#define REP_HEAD_LINES 10
#define TOTAL_HOURS    24
#define TOTAL_DAYS     7
#define MAX_FAILURES   20
#define TESTID_SIZE    30
#define PERC           37
#define BUFF_SIZE      255

#define EVENT         "Event"
#define TESTRESULT    "TESTRESULT"
#define TESTID        "TESTID"
#define REPHEAD_CHAR  "*"

char TestID [50];
int  h_event_count = 0;

/* -------------------------------- */
/* Function Prototype Declerations  */
/* -------------------------------- */

static int P1010_parse_rawreport_data  ( void );

int main( int argc, char **argv )
{

   int h_status      = 0,
  h_end_hour    = 0;

   if ( argc <= 1 ) {

       printf ("<H4>ERROR: No Test Failure Type Specified In Options !</H4><BR></BR>\n");
       printf ("<H4>Example: Options: -rn TESTFAILS 1%cr17 \n</H4>", PERC );
  h_status = -1;

   } else {

       strcpy ( TestID, argv[1] );

       if ( h_status == 0 ) {

           h_status = P1010_parse_rawreport_data ();
                 
       }
   }

   return (h_status);
}


static int P1010_parse_rawreport_data ()
                   
{
   int h_status      = 0,
  h_index       = 0,
  h_fail_tally  = 0,
  h_day_count   = 0,
  h_fail_count  = 0;

   char h_line_data   [255],
        h_test_fail   [255],
   h_rep_heading [255],
   h_event_date  [6],
   h_last_date   [6];

   /* ----- */
   /* STDIN */
   /* ----- */

   h_event_date  [0] ='\0';
   h_last_date   [0] ='\0';

   while( fgets( h_line_data, BUFF_SIZE, stdin ) != NULL ) {


  /* ----- */
       /* EVENT */
       /* ----- */

       if ( strncmp ( h_line_data, EVENT, 5 ) == NULL ) {

           if ( h_day_count > TOTAL_DAYS ) {

     printf ( "<H4>ERROR: Report request exceeded %d Days</H4>\n", TOTAL_DAYS );
               h_status = -1;
     break;
           }

      strncpy ( h_event_date, &h_line_data [8], 6 );

      if ( h_event_count == 0 ) {
          strcpy  ( h_last_date, h_event_date );
      }

      ++h_event_count;

       } /* EVENT */

  /* ------------------------------- */
       /* TESTRESULT, Extract the TESTID  */
  /* ------------------------------- */
       
       if ( strncmp ( h_line_data, TESTRESULT, strlen ( TESTRESULT ) ) == NULL ) {
      h_test_fail [0] = '\0';

      strncpy ( h_test_fail, &h_line_data [13], TESTID_SIZE );

      for ( h_index =0; h_index <= strlen (h_test_fail); h_index ++ ) {

          if ( h_test_fail [h_index] == ',' ) {
               h_test_fail [h_index] = '\0';
                    h_index = 0;          
               break;
     }
      }

      /* --------------------- */
      /* Find match on test ID */
      /* --------------------- */
  
      if ( strcmp ( h_test_fail, TestID ) == NULL ) {

printf ("Matched %s with %s : event date = %s last date = %s\n",
h_test_fail, TestID, h_event_date, h_last_date );

               ++h_fail_tally;

          if ( strcmp ( h_event_date, h_last_date ) != NULL ) {

                   printf ("Date: %s\tFailure Type = %s\tQty = %d\tEvents = %d\n",

         h_last_date,  TestID,
         h_fail_tally, h_event_count );
                   h_fail_tally  = RESET;
         h_event_count = 0;
         ++h_day_count;

                   printf ("date changed \n" );
      
     }  

     }  /* TestID match on */
       }     /* TESTRESULT      */
   }        /* STDIN           */

   return ( h_status );

}

Answer
I found few problems with your code.
You have used strcmp() function at many places. But the usage is wrong.
strcmp() returns an integer. But you are comparing this with pointer (NULL)! So, this would give wrong result.

I am not telling that this is the cause of the bug. But before going ahead we must correct all the mistakes, otherwise we would be wasting our time in something which can be a side effect of this mistake.
So, please correct these errors and post the correct code. Then I will try to get the bug fixed.

-ssnkumar

P.S: Please read the manual page for strcmp().

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.