c语言write 参数,fwrite()参数有关问题

fwrite()参数问题求助

本帖最后由 nain001 于 2013-05-22 22:14:47 编辑

typedef struct student{

char number[11];

char name[11];

char sex;

int age;

}Item;

typedef struct node{

Item item;

struct node * next;

}Node;

typedef  Node * List;

/*  将List写入文件  */

void WriteToFile(const List * plist,char * filename){

Node * pnode = *plist;

FILE * fp;

int count=0;

if((fp = fopen(filename,"w+b"))==NULL){

fprintf(stderr,"%s 无法打开!,写入文件失败.\n",filename);

exit(1);

}

while(pnode!=NULL){

fwrite(pnode->item,sizeof(Item),1,fp);//这行报错,参数不对,要怎么改呢?第一个参数不可以这样吗?

pnode = pnode -> next;

count++;

}

fclose(fp);

printf("有%d条记录被写入文件\"%s\"中.\n",count,filename);

}

分享到:

------解决方案--------------------

fwrite(&(pnode->item),sizeof(Item),1,fp);

应该是传递地址

------解决方案--------------------

hi, dear friend.

i try to compile your code and print error message below:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

test.c:29:9: error: incompatible type for argument 1 of ‘fwrite’

In file included from test.c:1:0:

/usr/include/stdio.h:717:15: note: expected ‘const void * __restrict__’ but argument is of type ‘Item’

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

size_t fwrite(const void *ptr, size_t size, size_t nmemb,

FILE *stream);

===base these info, can you understand the reason?  (you pass a param pnode->item type is Item but the fwrite need accept a const void *)

to solve this problem change the code : Item item  ----- Item * item

hope can help you !

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值