【发布时间】:2015-09-21 00:13:02
【问题描述】:
我正在尝试解析文本文档并将每一行的数据放入 char 字符串的二维数组中。但是,文件关闭后,我的二维数组的每一行都是最后一行数据的副本。就好像最后一行覆盖了所有以前的数据。例如,array[0][1] 打印与 array[19][1] 相同的值,即使 array[0][1] 应该是不同的值。
C 代码:
int h = 0;
const char* array[20][5];
while(fgets(line, sizeof(line), fp) != NULL)
{
sscanf(line,"%s%*[^\n]",firstWord);
if (strcmp("transition",firstWord) == 0)
{
sscanf(line,"%s%s%s%s%s%s",garbage,temp0,temp1,temp2,temp3,temp4);
array[h][0] = temp0;
array[h][1] = temp1;
array[h][2] = temp2;
array[h][3] = temp3;
array[h][4] = temp4;
h++;
}
}
fclose(fp);
文本文档示例:
transition 0 1 3 1 R
transition 3 0 4 0 R
transition 1 _ 7 _ R
transition 0 _ 5 _ R
transition 0 0 1 x R
transition 1 1 2 x L
transition 2 0 2 0 L
transition 2 1 2 1 L
transition 2 x 2 x L
transition 4 0 4 0 L
transition 4 1 4 1 L
transition 4 _ 4 _ L
transition 4 x 4 x L
transition 1 0 1 0 R
transition 1 x 1 x R
transition 3 1 3 1 R
transition 0 x 0 x R
transition 7 _ 7 _ R
transition 2 _ 0 _ R
transition 3 _ 6 _ R
【问题讨论】: