Forrest Brown
Forrest Brown

Reputation: 23

Program compiles but runs forever and stops at function being called

Program compiles in gcc but goes blank and runs forever right before countEven() is called. I believe there is an issue reading in the file butI'm not too sure. I can't seem to get over this problem and it seems to be persistent amongst other problems I will upload later. I have a few more specific problems I need help with tonight, if someone has time and wants to help me.

The function is designed to read in a file, tokenize the entire file and check each token to see if it is an even number.

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

unsigned int countEven(char strFileName[])
{
    FILE *pFile;
    char buffer[80];
    char delim[] = " ,\t\n";
    char* token;
    int numb;
    unsigned int count = 0;
    
    pFile = fopen(strFileName, "r");
    
    if (pFile == NULL)
        printf("ERROR: the file is invalid");
    
    while(fgets(buffer, sizeof(buffer), pFile) != NULL)
    {
        token = strtok(buffer,delim);
        
        while(token != NULL)
        {
            numb = atoi(token);
            
            if((numb%2)==0)
                count++;
            
            token = (NULL, delim);
        }
        
        fclose(pFile);
    }
    
    return count;
}

int main(int argc, char* argv[])
{
    unsigned int numbEven = 0;
  
    printf("the file is %s \n", argv[1]);
    numbEven = countEven(argv[1]);
   
    printf("the number of even digits is %u \n", numbEven);
    return(EXIT_SUCCESS);
}

Upvotes: 1

Views: 191

Answers (1)

paxdiablo
paxdiablo

Reputation: 881153

token = (NULL, delim);

This is a statement involving the comma operator(a) that basically sets token to point at the delim string, hence it will never become NULL, resulting in an infinite loop. What you need there is:

token = strtok(NULL, delim);

In addition to that, you're closing the file within the loop that reads and processes lines of that file (or you would be had you not got stuck in the infinite loop). You probably want to do that after the loop (swap the fclose with the closing brace immediately after it).

It may also be a good idea to check argc before blindly using argv in main(). As it stands, running your program without any filename argument will most likely cause a problem since argv[1] will be NULL.

And, finally, a couple of other things to be wary of:

  • This won't work well if you have lines greater than 78 characters (you need space for the newline and string terminator) characters. Specifically, if you have a number like 1234|567 that's on that boundary (| is the boundary), you will get an even state for the first part of it despite the fact it's odd;
  • Any non-numeric tokens will be turned into zero by atoi.

Neither of those will matter if you only have numeric tokens and your lines are short enough but it's something to keep in mind.


(a) The expression a, b evaluates both a and b but the result is simply b.

Upvotes: 2

Related Questions