1. When I try to add onto a file that I created, it works fine, but when I reopen that file it comes up with this.

PLEASE HELP ME!!!

Then, author and genre do the same thing

Here is what my code looks like...

/* Lab E */

/************************ Library *****************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/************************ Function Decleration **************************/
void organize(struct books [], int, FILE *, char[]);
struct books input();

struct books
{
    char title[40];
    char author[30];
    char genre[25];
    int pages;
    int rank;
}

/************************ Main Function ***************************/
main()
{
    FILE *fp;
    struct books book[30];
    int index = 0;
    char repeat;
    char filename[25];
    char filename2[25];
    int choice;
    int choice2;
 
    printf("What would you like to do? ");
    printf("\n1. Create a new file");
    printf("\n2. Open a previous file");
    printf("\n3. Add onto a previous file\n\n");
    scanf("%d",&choice);
    getchar();
 
    if(choice == 1)
    {
        printf("\n\nEnter in a new file name:  ");
        gets(filename);
     
        if((fp = fopen(filename, "w")) == NULL)
        {
            puts("Cannot open the file");
            exit(1);
        }
    }
 
    else if(choice == 2)
    {
        printf("What file name would you like to open?:  ");
        gets(filename);
         
        if((fp = fopen(filename, "r")) == NULL)
        {
            puts("Cannot open the file");
            exit(1);
        }
     

       
        while(fread(&book[index], sizeof(book[index]), 1, fp)==1) 
    {
                            
        printf("\nTitle:  %s\n", book[index].title);
        printf("Author   %s\n", book[index].author);
        printf("Genre:   %s\n", book[index].genre);
        printf("Pages:   %d\n", book[index].pages);
        printf("Rank:   %d\n", book[index].rank);
    }
        fclose(fp);
    }
 
    else if(choice == 3)
    {
        printf("What file name would you like to add onto?:  ");
        gets(filename);
     
        if((fp = fopen(filename, "r")) == NULL)
        {
            puts("Cannot open the file");
            exit(1);
        }
     
        while(fread(&book[index], sizeof(book[index]), 1, fp)==1)
        {
        index++;
        }
     
        fclose(fp);
     
        printf("\n\nWhat would you like to do?:  ");     
        printf("\n1. Overwrite the file");
        printf("\n2. Add onto the previous file\n\n");
        scanf("%d",&choice2);
     
        if(choice2 == 1)
        {
            printf("Enter in the file name:  ");
            getchar();
            gets(filename2);
         
            if((fp = fopen(filename, "w")) == NULL)
            {
                puts("Cannot open the file");
                exit(1);
            }
        }
     
        else if(choice2 == 2)
        {
            printf("Enter in the file name:  ");
            getchar();
            gets(filename2); 
         
            if((fp = fopen(filename2, "r")) == NULL)
            {
                puts("Cannot open the file");
                exit(1);
            }

            index = 0;
             while(fread(&book[index], sizeof(book[index]), 1, fp)==1) 
             {
                 index++;
             }

            fclose(fp); // closing the reading function
         
                if((fp = fopen(filename2, "w")) == NULL)
                {
                       puts("Cannot open the file");
                    exit(1);
                }
         
                    do
                    {
                        input(book[index]);
                        fwrite(&book[index], sizeof(book[index]), 1, fp); 
                        printf("\n\nDo you want to enter in another book?: ");
                        getchar();
                        scanf("%c",&repeat);                  
                        getchar();
                    }
                    while(repeat == 'y' || repeat == 'Y');
                   
            fclose(fp);//closing the writing function
        }
   }
    do
    {
        book[index] = input();
         
        printf("\n\n\nTitle: %s", book[index].title);
        printf("\nAuthor: %s", book[index].author);
        printf("\nGenre: %s", book[index].genre);
        printf("\nPages: %d", book[index].pages);
        printf("\nRank: %d", book[index].rank);
     
        fwrite(&book[index], sizeof(book[index]), 1, fp); 

        index++;
 
        printf("\n\nDo you want to enter in another book?: ");
        getchar();       
        scanf("%c",&repeat);
        getchar();
  printf("_______________________________________________________\n");
    }
    while(repeat == 'y' || repeat == 'Y');
    fclose(fp);
 
    organize(book, index, fp, filename);
 

}

/*************************** Sort the Books *****************************/
void organize(struct books book1[30], int index1, FILE *fp, char filename[25])
{
    int index = 0;
    int pass;
    int choice = 0;
    int booknum = 0;
    struct books temp;

    printf("\n\nHow would you like these organized?");
    printf("\n1. Author");
    printf("\n2. Genre");
    printf("\n3. # of Pages");
    printf("\nType 1, 2, or 3: ");
    scanf("%d",&choice);
     
    if (choice == 1)
    {
        for(pass = 1; pass < index1; pass++)
            for(index = 0; index < index1; index++)
                if(strcmp(book1[index].author, book1[index+1].author)>0)
                {
                    temp = book1[index];
                    book1[index] = book1[index + 1];
                    book1[index + 1] = temp;
                }
     for (index = 0; index < index1; index++)
        printf("\n\n\tAuthor: %s\n\tTitle: %s\n\tGenre: %s\n\tPages: %d\n\tRank: %d", book1[index].author, book1[index].title, book1[index].genre, book1[index].pages, book1[index].rank);
    }
 
    else if (choice == 2) 
    {
        for(pass = 1; pass < index ; pass++)
            for(index = 0; index < index1-1; index++)
                if(strcmp(book1[index].genre, book1[index+1].genre)>0)
                {
                    temp = book1[index];
                    book1[index] = book1[index + 1];
                    book1[index + 1] = temp;
                }
        for (index = 0; index < index1; index++)
        printf("\n\n\tGenre: %s\n\tTitle: %s\n\tAuthor: %s\n\tPages: %d\n\tRank:%d", book1[index].genre, book1[index].title, book1[index].author, book1[index].pages, book1[index].rank);
    }
 
 
    else if (choice == 3)
    {
        for(pass = 1; pass < index ; pass++)
            for(index = 0; index < index1; index++)
                if(book1[index].pages > book1[index+1].pages)
                {
                    temp = book1[index];
                    book1[index] = book1[index + 1];
                    book1[index + 1] = temp;
                }
  for (index = 0; index < index1; index++)
        printf("\n\n\tPages: %d\n\tTitle: %s\n\tGenre: %s\n\tAuthor: %s\n\tRank: %d", book1[index].pages, book1[index].title, book1[index].genre, book1[index].author, book1[index].rank);
    }
}
/************************** Input Function ***************************/
struct books input()
{
            struct books book;

            printf("                      Book Organizer\n");
            printf("\nEnter in the book title: ");
            gets(book.title);
            printf("Enter in the book author: ");
            gets(book.author);
            printf("Enter in the genre: ");
            gets(book.genre);
            printf("Enter in the number of pages: ");
            scanf("%d",&book.pages);

            do
            {
             printf("Enter in the book ranking (1-10): ");
             scanf("%d",&book.rank);
            }    
            while(book.rank < 0 || book.rank > 11);     
           
            return(book);
}