在 strtok 上出现分段错误,我将输入字符串lyne
定义为 char 数组而不是指针,但似乎不起作用。这是在 C 和 linux 中
typedef struct
{
int x;
char *y;
} child;
typedef struct{
child *details;
} parent;
fp = fopen(filename,"r"); // read mode
char lyne[25];
char *item;
fgets(lyne,25,fp);
parent record;
record.details= malloc (5 * sizeof(child));
while (fgets(lyne,25,fp)) {
printf("test %s \n",lyne);
item = strtok(lyne," ");
strcpy(record.details->y,item);//seg error on this line
}
fclose(fp);
my file looks like this
file#1
ABC 100
BCE 200
OUTPUT:
test ABC 100
Segmentation fault