I'm using an array of pointers to pass the inputted values to a text file but when I use fputs I keep getting the error "expected const char*", and as the array of pointers is defined from the struct named books it is of the type "struct books *". I tried using the puts statement but that doesn't solve the problem either. Would it be better not to use pointers?
const char *BOOKS = "books.txt";
struct Books{
int isbn;
char title[25];
char author[20];
char status[10];
}*a[MAX];
int main (void)
{
int i;
printf("Enter the books details that you currently have:\n\n");
for(i=0;i<MAX;i++)
{
printf("Enter the book's isbn number:");
scanf("%d", &a[i]->isbn);
printf("Enter the book's title :");
scanf("%s", &a[i]->title);
printf("Enter the book's author:");
scanf("%s", &a[i]->author);
printf("Enter the book's status, whether you 'have' or whether it is 'borrowed':");
scanf("%s", &a[i]->status);
}
FILE *fp = fopen(BOOKS, "r+" );
if (fp == NULL )
{
perror ("Error opening the file");
}
else
{
while(i<MAX )
{
fputs( a[i]->status, fp);
fputs(a[i]->author, fp);
fputs( a[i]->title, fp);
fputs( a[i]->isbn, fp);
}
fclose (fp);
}
}
a[i]->isbndereferences a null pointer. It would be simpler to use an array of Book instead .