我收到的文件包含1,2;3,4;0,4;2,5
之类的字符串。这些是由;
分隔的键 - 值对。当我在下面的代码中运行时,我可以看到一切看起来都很好,我得到了正确的键值对,但是当我实际上尝试将它们插入到数组中时,我得到了分段错误。任何建议或建议如何解决这个问题将不胜感激。
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE *f = fopen("test.txt", "rb");
fseek(f, 0, SEEK_END);
int fsize = ftell(f);
fseek(f, 0, SEEK_SET);
int n[fsize+1];
char *string = malloc(fsize + 1);
fread(string, fsize, 1, f);
fclose(f);
int first;
int second;
char* first_split;
char *rest2;
char *rest;
while(first_split = strtok_r(string, ";", &rest))
{
char * second_split = strtok_r(first_split, ",", &rest2);
first_split = rest2;
first = atol(second_split);
second = atol(first_split);
printf("n[%d]=%d " ,first, second);
/* n['first']=second; */
string = rest;
}
}