fwrite() not working to write integer in binary file

后端 未结 4 968
暖寄归人
暖寄归人 2021-01-16 23:25

Can someone tell my why won\'t this function work? I just can\'t get it...

void writeRegister(FILE *arq, Book *L){ //writes in actual file position
  char          


        
4条回答
  •  说谎
    说谎 (楼主)
    2021-01-17 00:07

    The first parameter of fwrite expects a pointer.

    Lines such as the following:

    fwrite(L->PAGES, sizeof(int), 1, arq);
    

    Should be written as follows:

    fwrite(&(L->PAGES), sizeof(int), 1, arq);
    

    Sames goes for YEAR and PRICE members of that struct

    fwrite(&(L->YEAR), sizeof(int), 1, arq);
    ...
    fwrite(&(L->PRICE), sizeof(float), 1, arq);
    

    Note, you don't need to make the same change for TITLE, PUBLISHER, and AUTHOR because the type of those member fields are already pointers (char*).

提交回复
热议问题