Hello everyone,
I am having a problem with the output of a file. My function does everything it needs to do with the exception of one line. I have a .dat file with the following:
Code:
joe clint james brint howard
jimmy
alexander
*
me
When I run the function my output is as such:
Code:
file opened
word: joe
digit: 3
word: clint
digit: 5
word: james
digit: 5
word: brint
digit: 5
word: howard
digit: 7 <---------------------------------Here is the problem
word: jimmy
digit: 5
word: alexander
digit: 9
ERROR non alphanumeric digit on line 4!
File closed
"Howard" is only six digits in length. Also it seems to skips a line in the output which could be the extra character but I am not sure. What I find very interesting is the other names, (jimmy & alexander) on separate lines do not have that problem.
Here is my function:
Code:
#include "main.h"
#include "fileCheck.h"
int fileCheck(FILE *fp)
{
int line_count = 0;
int ret_val = 0;
int digit_count = 0;
char file[BUFF];
regex_t regex;
char *pch;
if (regcomp(®ex, to_find, REG_EXTENDED | REG_NEWLINE) != 0)
{
fprintf(stderr, "Failed to compile regex '%s'\n", to_find);
return EXIT_FAILURE;
}
while (fgets(file, BUFF, fp) != NULL)
{
++line_count;
if ((ret_val = regexec(®ex, file, 0, NULL, 0)) == 0)
{
printf("ERROR non alphanumeric digit on line %d!\n", line_count);
return EXIT_FAILURE;
}
pch = strtok(file, " \n\r\t");
while (pch != NULL)
{
digit_count = strlen(pch);
printf("word: %s\n", pch);
printf("digit: %d\n", digit_count);
pch = strtok(NULL, " ");
}
}
}
I for the life of me cannot figure it out. Any ideas on how I can correct this? I am aware of sscanf to parse the line but I do not know the format of the file. All I know is the file will be alphanumeric and if there is a non-alphanumeric, I must print out an error with the line it occurred on such as the above output.
Thanks for your help.