I am trying to write from file to structure in C. Whenever I try to assign value in structure it gives me error: incompatible types in assignment.
My structure looks like this:
struct competition{
char eventTitle[79];
char date[79];
char time[79];
};
Basically I want to open the file and assign individual line to different value in structure. ie. first line in file -> eventTitle, second line -> date, third line -> time.
Here is how I try to assign it:
FILE *naDaSt;
char *mode = "r";
int lines = 0;
char line[79], current[79];
naDaSt = fopen(nameDateStart, mode);
if(naDaSt == NULL){
printf("Can't find the files.");
exit(1);
}else{
struct competition comp, *p;
p = ∁
while(fgets(line, 79, naDaSt)){
lines++;
if(lines == 1){
p->eventTitle= line;
}
if(lines == 2){
p->date = line;
}
if(lines == 3){
p->time = line;
}
}
}
}
Could anyone help me?
Root Cause:
You cannot do this:
You are trying to assign arrays using assignment which cannot be done, arrays need to be copied. The compiler rightly reports the error:
because indeed the type you are trying to assign are incompatible.
Solution:
Instead, You need to use strcpy and copy the character arrays and not assign them.