2d数组 - 每行被Next覆盖

时间:2015-09-23 19:24:58

标签: c arrays 2d overwrite

我正在从文件中读书。每当我读到过渡'我试图添加看似2d数组的下5个元素。我有一堆printf,我已经注释掉了这表明我正在添加适当的元素。但是,一旦我从文件中读完,我就会循环检查我的2d数组值,并且它们都被最后一行的值覆盖了。

这是我的代码

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]);


    }
}

如果我正在阅读的文件如下所示,

转换0 x 0 x R

过渡0 1 4 1 L

过渡0 0 1 x R

我希望数组看起来像这样

0x0xR

0141L

001xR

相反,数组的值将是

001xR

001xR

001xR

我知道覆盖发生在每一行。即:当我写入第2行时,值变为:

0141L

0141L

我真的迷失了如何修复它。

1 个答案:

答案 0 :(得分:0)

问题在于我对strtok()的错误使用。我接受了Dmitri的建议,并使用strdup()为新字符串分配内存,并且我得到了正确的结果。

nextWord = strtok(NULL, "\t");
temp = strdup(nextWord);
transition[transCount][0] = temp;