First (read comments):
you have declared char temp[1];
one size it has to be of size 2
according to your code(otherwise undefined behavior because memory overrun):
char temp[2];
while (isdigit(ch)) { // from `(` to `)`
temp[0] = ch; // should be a null terminated
temp[1] = '\0'; // add this step;
strcat(numberAppended, temp);
ch = fgetc(&file);
}
Second: your numberAppended
is parse to a string of kind: "0 9 500 3"
and your are calling
number = (int)strtol(numberAppended, &end, 0);
^
output argument
syntax for strtol:
long int strtol(const char *numberAppended, char **end, int base);
Where
numberAppended
: is the string to be converted into a long integer.end
: points to a pointer that will be set to the character immediately following the long integer in the string "numberAppended
".
And your are to write something like this: (read comments)
end = numberAppended; // assign to first string
// in a loop {
number = (int)strtol(end, &end, 0); // in loop end is input &end is output
printf("The number is %d\n",number);
//}
My following code will help your to understand how to use strtol()
to parse and extract number from numberAppended
string:
#include <stdio.h> /* printf */
#include <stdlib.h> /* strtol */
int main (){
char numberAppended[] = "2001 11 223 444 566";
char * end;
long int li;
end =numberAppended;
int base =10;
int ele = 0;
while(li=strtol (end, &end, base)){
printf("%ld \n", li);
ele += 1;
}
printf("\nNo of elements: %d", ele);
return 0;
}
output:
2001
11
223
444
566
No of elements: 5
third: may be its not an error but I couldn't find where processIndex
updates in your code before switch(){}
..