So I have this code below, in my code i load a bunch of movies into an array of structures, Now i know the problem is happening in here (strcpy(parts[x].rating,sp); and what it looks like the strcpy is not working properly, can anyone give me a hint or something useful i can use to solve the problem? I have tried doing it without the strcpy but it doesn't work either !
struct movies
{
char *name;
char *rating;
int time;
float rtwo,rone,rthree;
};
void displayData(movies *);
main ()
{
struct movies parts[6];
FILE *fp;
char line[100];
fp=fopen("movies.csv","r");
if (fp == NULL)
{
printf("Could not locate file");
exit(0);
}
char *sp;
int x = 0;
while (fgets(line,100,fp)!=NULL)
{
sp=strtok(line,",");
strcpy(parts[x].name,sp);
sp=strtok(NULL,",");
strcpy(parts[x].rating,sp);
sp=strtok(NULL,",");
sscanf(sp,"%d", &parts[x].time);
sp=strtok(NULL,",");
sscanf(sp,"%f", &parts[x].rone);
sp=strtok(NULL,",");
sscanf(sp,"%f", &parts[x].rtwo);
sp=strtok(NULL,",");
sscanf(sp,"%f", &parts[x].rthree);
++x;
}
fclose(fp);
displayData(parts);
return 0;
}
void displayData(movies *parts)
{
for (int x=0; x < 6 ; ++x)
{
printf("\n Name: %s RATED: %s Time:%d crit: %.1f crit: %.1f crit:
%.0f",parts[x].name,parts[x].rating,parts[x]
.time,parts[x].rone,parts[x].rtwo,pa
rts[x].rthree);
}
}
parts[x].nameis a pointer, what does it point to?char, though. It is not relevant where the array is contained.