我需要在结构为持久即希望将其存储在一个文件中,需要读取字符的字符的数据文件存储结构......为此,我写了下面的代码...下面的代码是不工作也无法写入结构到文件(逐个字符)...... 我需要一个字符为单位关于使用读写
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));
我可以用FREAD & fwrite的.. 。但我想按字符做这个字符...所以我正在使用读取&写(他们是直接系统调用仪式)...我无法写入它我写功能显示错误我.E它返回-1 ...这有什么错在上面的代码...
如果一个系统调用(LI ke'read'或'write')返回'-1',这意味着有什么错误。通过检查['errno'](http://en.cppreference.com/w/c/error/errno)可以发现_what_错误。 –
我越来越差的文件描述符错误...这是什么意思? @JoachimPileborg – Rohit
请说明你如何打开文件。 – ElPaco