6 Replies - 1281 Views - Last Post: 12 May 2012 - 07:55 AM Rate Topic: -----

#1 mccabec123  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 214
  • Joined: 03-March 11

Multiple records in a binary file, search etc.

Posted 11 May 2012 - 03:47 PM

Hey guys, I am creating a library book system, and I was just wondering how I go about sifting through a binary file of multiple records and searching based on a title, author or isbn number. Everytime I create a book, I store the struct that I'm using in the binary file, now I'm not sure if my code is overwriting the previous records or if I'm using the wrong function to search the file, if someone could give me some examples or some pointers it'd be greatly appreciated.

Here's my code so far:

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

int choice;

struct bookType
{
    char title[30];
    char isbn[13];
    char author[30];  
    
};

int search()
{  
    struct bookType book;
    int i = 0;
    char name[80];
    getchar();
    printf("Enter a book title to search: ");
    fgets(name, 80, stdin);
    FILE *BookFile = fopen("BookFile.bin", "rb+");
    rewind(BookFile);
    int counter;
    while(counter == 0)
    {
        counter = fread(&book, sizeof(book), 1, BookFile);
        if(strcmp(book.title, name) == 0)
        {
            printf("\nBook found, yey! \nThis is the book's title: %s", book.title);
            printf("\nThis is the book's author: %s", book.author);
            printf("\nThis is the book's ISBN: %s", book.isbn);
            i = 1;
        }
        
        else if(counter != 0)
        {
            printf("\nBook not found :(/>\n");
            i = 1;
        }
    }
    return 0;
}
void addBook()
{  
    struct bookType book;
    int i;
    
    i = 0;
    
    do
    {
        printf("Welcome to the add book section of this software.\n");
        
        printf("\nPlease enter the name of the book: ");
        getchar();
        fgets(book.title, 30, stdin);
        
        printf("\nPlease enter the book's ISBN number: ");
        fgets(book.isbn, 15, stdin);
        
        printf("\nPlease enter the name of the book's author: ");
        fgets(book.author, 30, stdin);
        
        FILE *BookFile = fopen("BookFile.bin", "ab+");
        if(!BookFile)
        {
            printf("Error opening BinFile.bin");
        }
        else 
        {
            fwrite(&book, sizeof(book), sizeof(book), BookFile);
            fclose(BookFile);
            FILE *BookFile = fopen("BookFile.bin", "rb+");
            fread(&book, sizeof(BookFile), 3, BookFile);
			printf("\nThis is the name of the book: %s", book.title);
            printf("\nThis is the isbn number: %s", book.isbn);
            printf("\nThis is the author's name: %s", book.author);
            fclose(BookFile);
        }
        
        char writeAnother;
        int writeAnotherCounter = 0;
        
        do
        {
            printf("\nWould you like to write another record? (y/n) ");
            scanf("%s", &writeAnother);
            
            switch(writeAnother)
            {
                case 'y':
                    i = 0;
                    writeAnotherCounter = 1;
                    break;
                case 'n':
                    i = 1;
                    writeAnotherCounter = 1;
                    break;
                default :
                    printf("Please enter a valid condition! \n");
                    break;
            }
        }while(writeAnotherCounter != 1);
        
        
    }while(i != 1);
}
int main(int argc, char *argv[])
{
    
    printf("Welcome to the library system developed by Conner McCabe.  \n");
    printf("-----------------------------------------------------------\n");
    printf("* Please make a selection from one of the options below:  *\n");
    printf("-----------------------------------------------------------\n");
    printf("*                                                         *\n");
    printf("*               1. Search for a Book                      *\n");
    printf("*                                                         *\n");
    printf("*               2. Add a Book to the Library              *\n");
    printf("*                                                         *\n");
    printf("*               3. Exit the Program                       *\n");
    printf("*                                                         *\n");
    printf("*                                                         *\n");
    printf("-----------------------------------------------------------\n");
    
    do
    {
        printf("\nPlease enter a number from 1 - 3: ");
        scanf("%d", &choice);
        
        switch(choice)
        {
            case 1:
                search();
                break;
            case 2:
                addBook();
                break;
            case 3:
                exit(0);
                break;
            default :
                printf("Please enter a valid condition! \n");
                break;
        }
    }while(choice != 1 || choice != 2 || choice != 3 );
    
    system("sleep");	
    return 0;
    
}


Is This A Good Question/Topic? 0
  • +

Replies To: Multiple records in a binary file, search etc.

#2 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 269
  • View blog
  • Posts: 3,821
  • Joined: 08-January 12

Re: Multiple records in a binary file, search etc.

Posted 11 May 2012 - 04:10 PM

http://www.dreaminco...to-binary-file/
I believe you started this, so why did you start a new one ??
Was This Post Helpful? 0
  • +
  • -

#3 mccabec123  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 214
  • Joined: 03-March 11

Re: Multiple records in a binary file, search etc.

Posted 11 May 2012 - 04:19 PM

I figured that the topic was no longer suitable for what we actually talking about, it had digressed a bit. Also I took a while to respond (had exams the past couple days :P so I've been otherwise engaged :D) So I figured that you guys wouldn't realise that I was still working on this, but good to see you're still here Aresh :P
Was This Post Helpful? 0
  • +
  • -

#4 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 269
  • View blog
  • Posts: 3,821
  • Joined: 08-January 12

Re: Multiple records in a binary file, search etc.

Posted 11 May 2012 - 04:44 PM

In lines 73 & 76, why are you specifying the number of items to be more than one ?? In line 76, you are trying to read more than 50 bookType objects !! I don't know how it is even working !! Apart from that, I had mentioned this previously also, whenever you add data and reopen it, the cursor is reset to start of file. It will always point to same object. And I have attached a screenshot of your program.. The name aresh was put by me on previous runs.. So obviously there is something wrong with your program !!!

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#5 mccabec123  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 214
  • Joined: 03-March 11

Re: Multiple records in a binary file, search etc.

Posted 11 May 2012 - 04:53 PM

Yes I know, I don't quite understand what's going on to be honest :/
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5667
  • View blog
  • Posts: 22,509
  • Joined: 23-August 08

Re: Multiple records in a binary file, search etc.

Posted 12 May 2012 - 04:28 AM

You are not taking into account the fact that fgets also reads and stores the terminating newline character. You must remove it. One way to do so:

char buffer[80] = { 0 };
fgets(buffer, sizeof(buffer), stdin);
/* Check if character before null terminator is a newline,
   if it is, replace with a null character.

   NOTE: If the the second-to-last character was not a newline, 
   it means that there wasn't enough space in the buffer for
   the entire string */
if (buffer[strlen(buffer) - 1] == '\n')
{
    buffer[strlen(buffer) - 1] = '\0';
}


The readLine function in this blog entry shows the use of fgets.
Was This Post Helpful? 0
  • +
  • -

#7 mccabec123  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 214
  • Joined: 03-March 11

Re: Multiple records in a binary file, search etc.

Posted 12 May 2012 - 07:55 AM

So this could be causing the error you think? I'm not at home right now, but I'll give this a go when I get back, thanks JackOfAllTrades :D Appreciate it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1