Skip to content

pc016.c

Problem Statement

Write a c program that records book data from user and stores them in to a file.

Metadata

Property Detail
Author Amit Dutta (amitdutta4255@gmail.com)
License MIT
Difficulty Advanced (index: 6 / 10)

Concepts

Beta Feature

This concept detection system is still in beta and may occasionally show incorrect or incomplete results.

  • Array
  • Pointers
  • Iteration
  • Sorting (possible)
  • Recursion

Actions

Raw View on GitHub

You can print or save this file by opening Raw and using your browser.

Source Code

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

#define FILENAME "Library.txt"

typedef struct Book
{
    int id;
    char title[20];
    char author[20];
}Book;

void dataInput(Book *, int);
void displayData(Book *, int);
bool writeLog(Book *, int);

int main()
{
    Book book[3];
    int choice;

    printf("== Enter the data ==");
    dataInput(book, 3);
    printf("\n\n Data enetered by the user ==");
    displayData(book, 3);

    if (writeLog(book, 3))
    {
        printf("\n\nSuccessfully written the log into \"%s\"", FILENAME);
        return 0;
    } else
    {
        printf("\nError writing log.");
        return 1;
    }
}

void dataInput(Book *book, int n)
{
    int i;
    char *p;

    for (i = 0; i < n; i++)
    {
        printf("\nEnter the Book ID: ");
        if (scanf("%d", &book[i].id) != 1)
        {
            printf("\nError reading bookID.");
            exit(1);
        }
        while (getchar() != '\n')
            ;

        printf("Enter the Book Title: ");
        if (fgets(book[i].title, sizeof(book[i].title), stdin) == NULL)
        {
            printf("\nError reading title.");
            exit(1);
        }
        p = strchr(book[i].title, '\n');
        if (p)
            *p = '\0';

        printf("Enter the Book Author: ");
        if (fgets(book[i].author, sizeof(book[i].author), stdin) == NULL)
        {
            printf("\nError reading Author.");
            exit(1);
        }
        p = strchr(book[i].author, '\n');
        if (p)
            *p = '\0';
    }
}

void displayData(Book *book, int n)
{
    int i;
    for (i = 0; i < n; i++)
    {
        printf("\n\nBook ID: %d"
               "\nBook Title: %s"
               "\nBook Author: %s",
               book[i].id, book[i].title, book[i].author);
    }
}

bool writeLog(Book *book, int n)
{
    int i;
    FILE *output = NULL;
    output = fopen(FILENAME, "w");
    if (output == NULL)
    {
        printf("\n\nError writing file \"%s\"", FILENAME);
        return false;
    }
    for (i = 0; i < n; i++)
    {
        fprintf(output, "%d \'%s\' \'%s\'\n", book[i].id, book[i].title, book[i].author
    }
    fclose(output);
    return true;
}

Explanation

Explain with AI

Copy the prompt below and paste it into any AI assistant.

    You are explaining a C programming code to a beginner.

    STRICT RULES:

    - Only use the given code. Do NOT assume anything not present.

    - Do NOT add extra examples.

    - Keep explanation clear and short.

    - If something is unclear, say "Not clear from code".

    - Follow the exact format below. Do NOT change headings.

    FORMAT:

    [START]

    ## What it does

    (Explain the overall purpose in 1-2 sentences)

    ## Step-by-step

    (Explain how the code works in steps, simple language)

    ## Key Concepts

    (List concepts like loop, condition, function, etc.)

    ## Notes

    (Mention any limitations, errors, or assumptions)

    [END]

    CODE (pc016.c):

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

    #define FILENAME "Library.txt"

    typedef struct Book
    {
        int id;
        char title[20];
        char author[20];
    }Book;

    void dataInput(Book *, int);
    void displayData(Book *, int);
    bool writeLog(Book *, int);

    int main()
    {
        Book book[3];
        int choice;

        printf("== Enter the data ==");
        dataInput(book, 3);
        printf("\n\n Data enetered by the user ==");
        displayData(book, 3);

        if (writeLog(book, 3))
        {
            printf("\n\nSuccessfully written the log into \"%s\"", FILENAME);
            return 0;
        } else
        {
            printf("\nError writing log.");
            return 1;
        }
    }

    void dataInput(Book *book, int n)
    {
        int i;
        char *p;

        for (i = 0; i < n; i++)
        {
            printf("\nEnter the Book ID: ");
            if (scanf("%d", &book[i].id) != 1)
            {
                printf("\nError reading bookID.");
                exit(1);
            }
            while (getchar() != '\n')
                ;

            printf("Enter the Book Title: ");
            if (fgets(book[i].title, sizeof(book[i].title), stdin) == NULL)
            {
                printf("\nError reading title.");
                exit(1);
            }
            p = strchr(book[i].title, '\n');
            if (p)
                *p = '\0';

            printf("Enter the Book Author: ");
            if (fgets(book[i].author, sizeof(book[i].author), stdin) == NULL)
            {
                printf("\nError reading Author.");
                exit(1);
            }
            p = strchr(book[i].author, '\n');
            if (p)
                *p = '\0';
        }
    }

    void displayData(Book *book, int n)
    {
        int i;
        for (i = 0; i < n; i++)
        {
            printf("\n\nBook ID: %d"
                   "\nBook Title: %s"
                   "\nBook Author: %s",
                   book[i].id, book[i].title,
    ... (truncated for brevity)