I need to make the data in the structure as persistent i.e wanted to store it in a file and need to read that character by character…For this i had written the below code…the below code is not working it is unable to write the structure into the file(character by character)…I needed that character by character
struct x *x1=(struct x*)malloc(sizeof(struct x));
x1->y=29;
x1->c='A';
char *x2=(char *)malloc(sizeof(struct x));
char *s=(char *)malloc(sizeof(struct x));
for(i=0;i<sizeof(struct x);i++)
{
*(x2+i)=*((char *)x1+i);
}
fd=open("rohit",O_RDWR);
num1=write(fd,x2,sizeof(struct x));
num2=read(fd,s,sizeof(struct x));
for(i=0;i<sizeof(struct x);i++)
printf(" %d ",*(s+i));
I can use fread & fwrite…but i wanted to do that character by character…so i am using read & write(they are direct system calls rite)…i am unable to write into it my write function is showing error i.e it is returning -1…Is there anything wrong in the above code…
Here are two functions you can use if you want: