I am trying to read a series of 8 integers from a file into an array then display those integers. I keep getting a segmentation fault the third time around, and I can't quite figure out what I am doing wrong.
struct aStruct {
int a;
int b;
...
};
typedef struct aStruct myStruct;
while(fgets(line, MAX_LENGTH, file) != NULL) {
int myArray[8] = {0};
char* val = strtok (line," ,\n\t\r");
while (val != NULL)
{
myArray[i] = atoi(val);
i++;
val = strtok (NULL, " ,\n\t\r");
}
myStruct foo;
foo.a = myArray[0];
foo.b = myArray[1];
...
}
The input file is structured like so:
0, 0, 1, 5, 0, 0, 0, 0
1, 0, 2, 5, 0, 0, 0, 0
2, 0, 3, 5, 0, 0, 0, 0
3, 0, 4, 5, 0, 0, 0, 0
4, 0, 5, 5, 0, 0, 0, 0
When tested with:
printf("myArray[0]: %d ", myArray[0]);
I get an odd output of 0 0
Where I believe it should be 0 1. Am I not initializing something correctly, or is my new syntax incorrect for the struct? I've tried a few different combinations, cant quite figure it out.
g++ -Wall -g) and use a debugger (e.g.gdb); alsonewis a C++ keyword (not a C one). So you should tag your question asC++(and improve your code to be more genuine C++, probably usingstd::vector..). Also show more code, notably declaration ofmyStruct...newis a C++ keyword. I am trying to write this all in C, so perhaps I should switch that around.newis some very weird macro, you should show its definition).