The following code is written to select some data from one file and copy into another, but I am getting error “Incompatible types in assignment” at notified position.I am unable to figure out what is causing this error. I will be glad to get your assistance.
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *myfile;
myfile=fopen("write.txt","a");
char *name;
int id,i=0,check,*w,n=0;
if(myfile)
{
do
{
printf("Enter ID and Name of the student\n");
scanf("%d%s",&id, name);
fprintf(myfile,"%d$ %s@", id, name);
printf("Are there any more students [y/n]");
}while(getch()=='y');
fclose(myfile);
}
printf("Do you want to shortlist the students [y/n]?");
if(getch()=='y')
{
myfile=fopen("write.txt","r");
while(check!=EOF)
{
check=getc(myfile);
i++;
if(check=='%')
n++;
}
fclose(myfile);
myfile=fopen("write.txt","r");
int x[i-1];
char car[n][20];
int yolo,y=0,q=0,j,h,temp;
for(j=0;j<i;j++)
{
x[j]=getc(myfile);
if(x[j]=='$')
{
w[q]=x[j-1];
yolo=temp=j;
q++;
}
else
if(x[j]=='@')
{
yolo++;
for(h=0;h<j-temp;h++)
car[y]=(char)x[yolo]; // ERROR
y++;
}
}
fclose(myfile);
//char data= new char[i++];
//fscanf();
myfile=fopen("shortlisted.txt","a");
if(myfile)
{
printf("Type the ID of the student you want to shortlist:\n");
scanf("%d",id);
}
}
else
printf("The file you specified doesn't exists");
printf("Hello world!\n");
return 0;
}
This code needs a little work at the moment but I want to remove all possible error before finalizing the code
Regards
car[y] is an array of chars whereas (char)x[yolo] is just a char. You’re trying to assign a char to an array of chars.
If you want to set the first character in car[y] to the value of x[yolo], just use car[y][0] = (char)x[yolo]. Note that that won’t convert x[yolo] into a char representation of an int. To get a char * representation of an integer, you’ll want to use itoa. But even then, a straight assignment won’t work: you’ll have to use strcpy.
EDIT: it has been brought to my attention that it’s better to use sprintf than itoa.