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();
}

Edited 3 Years Ago by cupin06

Why did you put all those pointers into the structure? You could have saved yourself a great deal of trouble by not using pointers in the structure. Each structure instance should hold the data for only one ebook. Then you can have an array of structures. For example:

struct ebook {
  char title[80];
  int edition;
  int year;
  char author[40];
  char publisher[80];
  double price;
  int page;  
};

struce ebook mybooks[2];

Now reading and writing the structure is very simple and accomplished in only one line of code

fwrite(mybooks, sizeof(struct ebook),2,fp);
fread(mybooks, sizeof(struct ebook),2,fp);

If you don't need all the ebooks in memory at the same time then you don't even need the array. Just read/write the structures on at a time inside the loop.

Edited 3 Years Ago by Ancient Dragon

I'm not clear what are u saying.But i already try modify the code.This is my new code.But have an error.Thanks for your helping.What wrong with my new code.This is the error

gcc skillBase.c -o skillBase
skillBase.c: In function ‘input’:
skillBase.c:31:1: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat]
skillBase.c:35:1: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat]
skillBase.c:39:1: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat]

My new code

#include <stdio.h>
#include <stdlib.h>

struct ebook {
  char title[80];
  int edition;
  int year;
  char author[40];
  char publisher[80];
  double price;
  int page;  
};


void input()
{

FILE *fp;
fp=fopen("ebook.bin", "wb");
struct ebook mybook[2];

int i;
for(i=0;i<2;i++){

printf("Enter ebook  title: \n");
scanf("%s",mybook[i].title);


printf("Enter ebook edition: \n");
scanf("%d",mybook[i].edition); //error at this line


printf("Enter ebook year: \n");
scanf("%d",mybook[i].year); //error at this line

printf("Enter ebook page: \n"); 
scanf("%d",mybook[i].page); //error at this line

fwrite(&mybook[i], sizeof(struct ebook),2,fp);

}
fclose(fp);
}

Edited 3 Years Ago by cupin06

This article has been dead for over six months. Start a new discussion instead.