Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4066486
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:08:09+00:00 2026-05-20T16:08:09+00:00

Hey! I am currently doing some prep for my upcoming college assignment but am

  • 0

Hey! I am currently doing some prep for my upcoming college assignment but am facing some difficulties.

I have to write an C database application that will store book details into a file, and allow me to recall them and delete them. I am having some problem with reading the file, the entries just won’t show when I list them all (Option 3).

I have had a look at the database file so I think it has been written to correctly.

Here is the basic function of my code:

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

// Global Varables
FILE *fp, *ft;

// Structs
typedef struct Books
{
    int id;
    char title[25];
    char forename[25];
    char surname[25];
    char isbn[13];
}Books;

void displayMenu()
{
    printf("Options");
}

void runapp()
{
    int option;
    char isbn[20];
    char deleteOrNot[2];
    Books book;

    fp = fopen("database.dat", "a+");
    if (fp == NULL)
    {
        printf("Failed to open file.");
    }

    long int booksize = sizeof(book);

    do
    {
        displayMenu();
        printf("Enter menu option: ");
        scanf("%i", &option);

        switch(option)
        {
            // =================================================================
            // -------- OPTION 1: Add a new book
            // =================================================================
            case 1:
                fseek(fp, 0, SEEK_END);

                printf("Please enter the books title: ");
                scanf("%s", book.title);
                printf("Please enter the authors first name: ");
                scanf("%s", book.forename);
                printf("Please enter the authors surname: ");
                scanf("%s", book.surname);
                printf("Please enter the ISBN number: ");
                scanf("%s", book.isbn);

                fwrite(&book, booksize, 1, fp);
                fflush(stdin);

            break;
            // =================================================================
            // -------- OPTION 2: Search and delete
            // =================================================================
            case 2:
               // Search and delete 
            break;
            // =================================================================
            // -------- OPTION 3: List all books
            // =================================================================
            case 3:
                rewind(fp);
                while(fread(&book, booksize,1, fp))
                    printf("%s", book.title);

                printf("\n\nPress any key to return to menu.");
                getch();
            break;
            // =================================================================
            // -------- OPTION 4: Exit the application
            // =================================================================
            case 4:
                fclose(fp);
                exit(0);
            break;
        }
        if (system("cls")) system("clear");
    }while(option != "\n");
}


int main()
{
    runapp();
    return 0;
}

Any help with solving this problem would be great!

Also feel free to give me some tips on my code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T16:08:09+00:00Added an answer on May 20, 2026 at 4:08 pm

    You’re trying to write the book structure directly to and from the file rather than a textual representation of the data, so you need to open the file in binary mode, (add b to the a+ in the fopen call). Otherwise your data could be slightly corrupted as the i/o library tries to translate carriage returns and line feeds to and from your host computer’s preferred convention.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey right now I'm using jQuery and I have some global variables to hold
Hey, i am currently working on a django app for my studies, and came
Hey, I've been developing an application in the windows console with Java, and want
Hey peoples, I've been studying Java for a couple of weeks, and have decided
Hey everyone, I'm using Virtual PC and working with a virtual hard disk (*.vhd)
Hey so what I want to do is snag the content for the first
Hey, I'm using Levenshteins algorithm to get distance between source and target string. also
Hey all, my Computational Science course this semester is entirely in Java. I was
Hey all. Newbie question time. I'm trying to setup JMXQuery to connect to my
Hey, in the Programming Pearls book, there is a source code for setting, clearing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.