Hi, I'm trying to write a program that will continuously pull up a text file, search it for anything new, and create/change a different text file if there are ever any differences in the "myfile.txt" that is being constantly searched. The problem is, when I run my code, it consumes all of my CPU. here's the code:

//Search for elements
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <time.h>

#define     TOTAL_KEYWORDS    12
#define     SEND_NO_MORE_THAN 15

#define     MAX_LISTINGS    150
#define     MAX_CHARS_PER_LINE    300


/**************************************************************************************
                 This function searches for a keyword in a line, if the keyword 
                 is found, it returns the position in the line array that the 
                 first letter of the match was found, otherwise, it returns -1. 
                 The "wild" character acts just like a blank tile in scrabble
                 (for the keyword)
*************************************************************************************/
int line_search(char line[], char keyword[], char wild);


/**************************************************************************************
                 This function searches a string for keywords. If any keyword is
                 found in the string, the string will go to the sent function.
                 Also, the character '~' can be used as a wild card character in
                 the keywords.
*************************************************************************************/
int search_for(char string_of_interest[MAX_CHARS_PER_LINE]);


/**************************************************************************************
                 This function creates the file "Send.txt", and puts the 
                 what_to_send string into it (starting where the actual
                 important text is)
*************************************************************************************/
void send(char what_to_send[MAX_CHARS_PER_LINE]);


char keywords[TOTAL_KEYWORDS][20], listings[MAX_LISTINGS][MAX_CHARS_PER_LINE];
char sent[SEND_NO_MORE_THAN][MAX_CHARS_PER_LINE];
int lines_sent=0, quit=0;

int main(){
    
    int i, j, index, hours, hit, day=0;
    char dummy[10];
    
    FILE *ifp, *ofp;
    
    printf("For how many hours do you want to run the program?");
    scanf("%d", &hours);
    getchar();
    
    //inialize the keywords
    for(i=0; i<MAX_LISTINGS; i++)
    memset(keywords[i], '\0', MAX_CHARS_PER_LINE );
    
    strcpy(keywords[0], "E. ORLANDO");
    strcpy(keywords[1], "E ORLANDO");
    strcpy(keywords[2], "EAST ORLANDO");
    strcpy(keywords[3], "UCF");
    strcpy(keywords[4], "E COLONIAL");
    strcpy(keywords[5], "E. COLONIAL");
    strcpy(keywords[6], "EAST COLONIAL");
    strcpy(keywords[7], "ALAFAYA");
    strcpy(keywords[8], "ROUSE");
    strcpy(keywords[9], "UNIVERSITY");
    strcpy(keywords[10],"AVALON");
    strcpy(keywords[11],"PEGASUS");
    
    while((clock ()/CLOCKS_PER_SEC)<(hours*3600)){

    ifp = fopen("myfile.txt", "r+");
        
    //initialize listings
    for(i=0; i<MAX_LISTINGS; i++)
    memset(listings[i], '\0', MAX_CHARS_PER_LINE );   
    
    day=0;
    while(day<2){ 
  
          //initialize listings
          for(i=0; i<MAX_LISTINGS; i++)
          memset(listings[i], '\0', MAX_CHARS_PER_LINE );
                 
          //Check the listings
          for(index=0; index<MAX_LISTINGS; index++){
          
                //Get the listing
                fscanf(ifp, "%[^\n]%c", listings[index], dummy);
                if(strcmp(listings[index],listings[index-1])==0){
                      fscanf(ifp, "%c", dummy);
                }
          
                //Make everything uppercase, to eliminate case-sensitivity
                for(i=0; i<MAX_CHARS_PER_LINE; i++){
                      listings[index][i]= toupper(listings[index][i]);
                }
          
          
                //This will keep track of the day
                if(line_search(listings[index], "<H4>", ' ')!=-1){
                      day++;
                      break;      
                }
          
                if(day==1){
                     //Search the listing for all the keywords, this function will also
                    //output the .txt file
                    hit = search_for(listings[index]);
                }
          
          }//closing the inner loop that does everything
    }//closing the outer loop so the file can be reopened
    
    fclose(ifp);
    
    }//closing clock loop
    
    return 0;

}



/**************************************************************************************
                 This function searches for a keyword in a line, if the keyword 
                 is found, it returns the position in the line array that the 
                 first letter of the match was found, otherwise, it returns -1. 
                 The "wild" character acts just like a blank tile in scrabble
                 (for the keyword)
*************************************************************************************/

int line_search(char line[], char keyword[], char wild) {
    
    int i, j, key_length, line_length, k=0;
    key_length = strlen(keyword);
    line_length = strlen(line);
    
    for(i=0; i<=(line_length-key_length); i++){
         for(j=0; j<key_length; j++){
                if (line[i+j]==keyword[j]||wild==keyword[j]){
                    k++;
                }
                else{
                    k=0;
                    break;
                }
         }
         if(k==key_length)
         return i;
    }
    return -1;
}



/**************************************************************************************
                 This function searches a string for keywords. If any keyword is
                 found in the string, the string will go to the sent function.
                 Also, the character '~' can be used as a wild card character in
                 the keywords.
*************************************************************************************/

int search_for(char string_of_interest[MAX_CHARS_PER_LINE]){
    
    int i, j, was_it_sent=0;
    
    //For each keyword
    for(i=0;i<TOTAL_KEYWORDS;i++){          
          
          //Search the string for
          if(line_search(string_of_interest, keywords[i], '~')!=-1){
                //If this is the first time anything is sent
                if(lines_sent==0){
                    //Send it on through
                    send(string_of_interest);
                    //Mark that it has just been sent
                    was_it_sent++;
                }
                //For every other time
                else{
                    //Check every other line that has been sent
                    for(j=0;j<lines_sent;j++){
                          //If it has been sent already
                          if(strcmp(string_of_interest, sent[j])==0){
                                //Mark that it was sent before
                                was_it_sent--;
                                break;
                          }
                    }
                    //If it hasn't been sent 
                    if(was_it_sent==0){
                         //Send it on through
                         send(string_of_interest);  
                         //Mark that it has just been sent
                         was_it_sent++;   
                    }   
                }//Out of the else statement
                break;
          }//Out of the if the keyword was found statement
    }//Out of the keyword loop
    return was_it_sent;
}



/**************************************************************************************
                 This function creates the file "Send.txt", and puts the 
                 what_to_send string into it (starting where the actual
                 important text is)
*************************************************************************************/

void send(char what_to_send[MAX_CHARS_PER_LINE]){
     
     FILE *ofp;
     
     int i;
     
     strcpy(sent[lines_sent], what_to_send);
     lines_sent++;
     printf("\n\nsend:\n");       
     ofp = fopen("Send.txt", "w");
     for(i=63;i<MAX_CHARS_PER_LINE;i++){
                    fprintf(ofp, "%c", what_to_send[i]);
                    printf("%c", what_to_send[i]);
     }
     printf("\n\n|%s|sent!!!!\n\n\n", sent[lines_sent-1]);
     
     fclose(ofp);
}

I feel like the problem might be that I'm not using any pointers, and the arrays are taking up all of the CPU's main registers. I've never had a problem like this before, though. Any advice would be greatly appreciated =)

Thanks,
Matt

line 75: while((clock ()/CLOCKS_PER_SEC)<(hours*3600)){

That is the problem (or majority of the problem). That line is not allowing any other process to run and just hogs up all the cpu time. Depending on the operating system put either Sleep() or sleep() to allow other processes to run.

A few other tweaks might improve speed a few clock ticks. For example
lines 87 and 88: you don't need that loop. memset(listings, '\0', MAX_LISTINGS * MAX_CHARS_PER_LINE ); lines 67-73 can be deleted. Just initialize the array where it's declared.

char *keywords[] = {
   "E. ORLANDO", "E ORLANDO", "EAST ORLANDO", < etc > 
};

it's totally due to that terrible hack of a "wait" routine. that's it and that's all.

the while loop is using every single available CPU cycle to approximate a timer. Like AD said, you need to use the library function for sleep(), in order to free up the CPU for other processes.

the other tweaks are good coding practice, but any speed gains won't even be noticeable compared to that behemoth of a cycle-eater you've got in that while() loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.