0

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);    
}
}
2
  • 1
    Storing a pointer in a file is almost always a very bad idea. Commented Mar 13, 2016 at 18:03
  • You made an array of pointers to Book, but you did not make them point anywhere yet. Writing a[i]->isbn dereferences a null pointer. It would be simpler to use an array of Book instead . Commented Mar 13, 2016 at 22:26

2 Answers 2

1

Assuming you have not given the complete code, So far I understand you want to write the structure elements to the FILE you have opened. In your for loop you need to use fputs some thing as follow,

fputs(a[i].title, fp);
fputs(a[i].author, fp);
fputs(a[i].status, fp);

Then it should work with out any error. Hope That Helps.

Sign up to request clarification or add additional context in comments.

2 Comments

I just tried this and I am still getting the same error. And yes, you're write, I do want to write the structure elements to the file I've opened.
@sc100 I have added an answer by modifying your code. Please have a look. If it works verify it by accepting to help others.
0

Hi I have modified your program as follow, Please have a look,

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;
    char *ISBN;
    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);
    }

    i = 0;

    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);
            itoa(a[i].isbn,ISBN,10); // Convert the isbn no to const char* in decimal format. to write in to the file.
            fputs( ISBN, fp);
            i++;   //Increment 'i' to access all the elements
        }
        fclose (fp);
    }
    return 0;
}

Hope this Helps.

1 Comment

You should explain what changes you made and why

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.