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);
}