The program i want to code is user will enter 2 ebook title,edition,year and pages.After that must write to binary file and read the binary file again to found the book with the highest pages.The first problem is why i cannot use this statement.
printf("Enter ebook title: \n");
scanf("%s",mybook.title[i]);
I want to give mybook.title[0] with the strings user input.So the value for the variable i will come from for loop.When i google i found must declare the statement below.
mybook.title[i]= malloc(100);
Now after i change my code the problem come when i want to read the binary file.Please help me.
This is my error
gcc skillBase.c -o skillBase
skillBase.c: In function ‘process’:
skillBase.c:61:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat]
This is my full coding.
#include <stdio.h>
#include <stdlib.h>
struct ebook {
char *title[2];
int *edition[2];
int *year[2];
char *author[2];
char *publisher[2];
double *price[2];
int *page[2];
};
void input()
{
FILE *fp;
fp=fopen("ebook.bin", "wb");
struct ebook mybook;
int i;
for(i=0;i<2;i++){
mybook.title[i]= malloc(100);
printf("Enter ebook title: \n");
scanf("%s",mybook.title[i]);
mybook.edition[i]= malloc(100);
printf("Enter ebook edition: \n");
scanf("%20d",mybook.edition[i]);
mybook.year[i]= malloc(100);
printf("Enter ebook year: \n");
scanf("%d",mybook.year[i]);
mybook.page[i]= malloc(100);
printf("Enter ebook page: \n");
scanf("%d",mybook.page[i]);
fwrite(&mybook, sizeof(mybook.title[i]),i,fp);
fwrite(&mybook, sizeof(mybook.edition[i]),i,fp);
fwrite(&mybook, sizeof(mybook.year[i]),i,fp);
fwrite(&mybook, sizeof(mybook.page[i]),i,fp);
}
fclose(fp);
}
void process()
{
int counter;
FILE *fp;
struct ebook mybook;
fp=fopen("ebook.bin","rb");
fread(&mybook,sizeof(mybook.page[0]),30,fp);
printf("%20d\n",mybook.page[0]);
fclose(fp);
}
main(void)
{
input();
process();
}