c - fwrite() not working to write integer in binary file -


can tell why won't function work? can't it...

void writeregister(file *arq, book *l){ //writes in actual file position   char c = '|';   int sizeregwrite = reglen(l); //reglen() returns size of book   fwrite(&sizeregwrite, sizeof(int), 1, arq);    fwrite(l->title, sizeof(char), strlen(l->title), arq);   fwrite(&c, sizeof(char), 1, arq); //writing delimiter    fwrite(l->author, sizeof(char), strlen(l->author), arq);   fwrite(&c, sizeof(char), 1, arq); //writing delimiter    fwrite(l->publisher, sizeof(char), strlen(l->publisher), arq);   fwrite(&c, sizeof(char), 1, arq); //writing delimiter    fwrite(l->year, sizeof(int), 1, arq);   fwrite(&c, sizeof(char), 1, arq); //writing delimiter    fwrite(l->language, sizeof(char), strlen(l->language), arq);   fwrite(&c, sizeof(char), 1, arq); //writing delimiter    fwrite(l->pages, sizeof(int), 1, arq);   fwrite(&c, sizeof(char), 1, arq); //writing delimiter    fwrite(l->price, sizeof(float), 1, arq);   fwrite(&c, sizeof(char), 1, arq); //writing delimiter         return; }   

the struct book declared this:

typedef struct {     char *title;     char *author;     char *publisher;     int year;     char *language;     int pages; float price; } book; 

main

int main() { file *arq = fopen("bd_books2.bin", "rb+"); if(arq == null)     printf("error while opening file!!!");  book l; readdata(&l); //reads fields keyboard , places in book. working writeregister(arq, &l);  system("pause"); return 0; 

}

i have use pointers inside struct, can't remove them. way, problem integers , float.

this fuction working if write integers , floats (year, pages , price) fprintf(), i'm writing in binary file, , of course want write in binary, i'm trying use fwrite().

another thing is: compiler pointing incompatible type argument 1 of 'fwrite' @ line: fwrite(l->price, sizeof(float), 1, arq);

can explain me happening? my program crashes when tries write in file...

the first parameter of fwrite expects pointer.

lines such following:

fwrite(l->pages, sizeof(int), 1, arq); 

should written follows:

fwrite(&(l->pages), sizeof(int), 1, arq); 

sames goes year , price members of struct

fwrite(&(l->year), sizeof(int), 1, arq); ... fwrite(&(l->price), sizeof(float), 1, arq); 

note, don't need make same change title, publisher, , author because type of member fields pointers (char*).


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -