0

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

    }
}
7
  • and yes i did #include <string.h> and all other necessary libraries . Commented Jun 15, 2017 at 22:24
  • There is no array in your structure! A pointer is not an array! Commented Jun 15, 2017 at 22:27
  • Given parts[x].name is a pointer, what does it point to? Commented Jun 15, 2017 at 22:39
  • @Olaf A structure array is this ( movies parts[6]; ), look at my declaration! Commented Jun 16, 2017 at 0:36
  • @AliSoujod: That's an array of structures, not a "structure array"! Details are important in programming - gt used to them. As a sidenote: you cannot copy a string to an array of structures: wrong types! You can copy a C string (which is only a convention, not a distinct type) to an array of char, though. It is not relevant where the array is contained. Commented Jun 16, 2017 at 8:24

2 Answers 2

1

The main issue is that you do not reserve target memory for the string you want to copy. Note that a struct movies-object provides a data member name, which allows to store a pointer to a sequence of character values, but not the character values themselfes. Hence, strcpy(parts[x].name,sp) accesses the (uninitialized) pointer parts[0].name (which is already UB by itself); the pointer points probably to "somewhere", and then you copy a string to "somewhere".

So you'd have to use malloc first, before calling strcpy; or you could use strdup, which does malloc and strcpy in one call:

    sp=strtok(line,",");  
    parts[x].name = malloc(strlen(sp)+1);
    strcpy(parts[x].name,sp);    

or:

    sp=strtok(line,",");  
    parts[x].name = strdup(sp);

Further, you should check if strtok actually returned something valid, e.g. like:

    if ((sp=strtok(line,",")) == NULL)
       continue;
    parts[x].name = strdup(sp);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, it WORKED ! I looked at not one or two, but multiple examples from stack-overflow but couldn't find a solution! now i understand that I have to allocate the memory before copying the string!
0

Been a while since I played with C, but, don't you need to malloc parts[x].rating before using it? Same thing with parts[x].name and any other char pointer you have.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.