I'm reading in from a file. Whenever I read in the word 'transition' I'm trying to add the next 5 elements that appear to a 2d array. I have a bunch of printf's which I have commented out that show that I am adding the proper element. However, once I finish reading in from file, I loop through to check my 2d array values and they have all been overwritten by the last row's values.
Here is my code
State* allStates[1002];
State* currentState;
char* input = argv[1];
char* inputStr = argv[2];
int maxTrans = atoi(argv[3]);
int transCount = -1;
FILE* inputFile;
char* transition[maxTrans][5];
inputFile = fopen(input, "r");
if (inputFile == 0)
{
perror("Can't open file\n");
exit(-1);
}
else
{
char next[1000];
//int counter = 0;
while (fgets(next, 1000, inputFile) != NULL)
{
char *nextWord;
nextWord = strtok_r(next, "\t");
if (strcmp(nextWord, "state") == 0)
{
//counter++;
nextWord = strtok_r(NULL, "\t");
//puts(nextWord);
int q = atoi(nextWord);
nextWord = strtok_r(NULL, "\n");
//puts(nextWord);
if (strcmp(nextWord, "accept") == 0)
{
State* newState = makeState(q, 1, 0, 0);
allStates[q] = newState;
}
else if (strcmp(nextWord, "reject") == 0)
{
State* newState = makeState(q, 0, 1, 0);
allStates[q] = newState;
}
else if (strcmp(nextWord, "start") == 0)
{
State* newState = makeState(q, 0, 0, 1);
allStates[q] = newState;
currentState = newState;
}
else
{
State* newState = makeState(q, 0, 0, 0);
allStates[q] = newState;
}
}
if (strcmp(nextWord, "transition") == 0)
{
//printf("\n");
//setup 2d array of transitions
transCount++;
nextWord = strtok_r(NULL, "\t");
//puts(nextWord);
transition[transCount][0] = nextWord;
//printf("%c", *transition[transCount][0]);
nextWord = strtok_r(NULL, "\t");
//puts(nextWord);
transition[transCount][1] = nextWord;
//printf("%c", *transition[transCount][1]);
nextWord = strtok_r(NULL, "\t");
//puts(nextWord);
transition[transCount][2] = nextWord;
//printf("%c", *transition[transCount][2]);
nextWord = strtok_r(NULL, "\t");
//puts(nextWord);
transition[transCount][3] = nextWord;
//printf("%c", *transition[transCount][3]);
nextWord = strtok_r(NULL, "\n");
//puts(nextWord);
transition[transCount][4] = nextWord;
//printf("%c", *transition[transCount][4]);
}
}
}
fclose(inputFile);
int u = 0;
int y = 0;
char m = 'm';
for (u; u < 12; u++)
{
printf("\n");
for (y = 0; y < 5; y++)
{
//transition[u][y] = &m;
printf("%c", *transition[u][y]);
}
}
If the file I'm reading in looks like this,
transition 0 x 0 x R
transition 0 1 4 1 L
transition 0 0 1 x R
I expect the array to look like this
0x0xR
0141L
001xR
Instead the values of the array will be
001xR
001xR
001xR
I know that the overwriting happens at each row. ie: when I'm writing to row 2 the values become:
0141L
0141L
I'm really lost as to how to fix it though.
The problem was with my incorrect usage of strtok(). I took Dmitri's advice and used strdup() which allocates memory for the new string and I'm getting the proper results.
nextWord = strtok(NULL, "\t");
temp = strdup(nextWord);
transition[transCount][0] = temp;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.