I am try to read a line from a file, separate the chars, and then read them to a malloc'ed int array. I seem to be getting a segmentation fault when I use atoi to cast the char to an int, and thus I am unable to assign the int to the array. Any help would be appreciated.
int main(int argc, char * argv[]){
FILE *in;
in = fopen(argv[1], "r");
FILE *out;
out = fopen(argv[2], "w");
int numDays;
char ignore[256];
fscanf(in, "%d", &numDays);
fgets(ignore, sizeof(ignore), in);
int *timeArray = (int *) malloc(numDays * sizeof(int)); ;
char buffer[256];
fgets(buffer, 256, in);
const char delimiter[2] = " ";
char *token;
token = strtok(buffer, delimiter);
int index = 0;
while( token != NULL ) {
printf( "%s\n", token);
token = strtok(NULL, delimiter);
int val = atoi(token);
timeArray[index] = val;
index++;
}
return(0);
}
tokenaNULLat that time? - underscore_d