---
title: "INVENTORY file of items"
subject: "Computer Programming"
chapter: 10
key: "semester_1/computer_programming:file-inventory"
source: https://www.bctengineer.com/q/semester_1/computer_programming/file-inventory
site: BCT Engineering
---

# INVENTORY file of items

Computer Programming · Chapter 10: Data Files

- Asked in: 2073 Chaitra, 6 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/file-inventory
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-10

## Question

Write a program to open file named INVENTORY and store in it for maximum 10000 data of ITEM_NAME, NUMBER, PRICE, QUANTITY. Extend the program to read this data from the above given filename and display the inventory table with the value of each item.

## Short answer

Open INVENTORY in write mode; while user enters 'y' and count<10000, read item name, number, price, quantity, write with fprintf. Reopen for reading, read with fscanf until EOF, display table with value = price × quantity.

## Answer

**Design.** Each item has ITEM_NAME, NUMBER, PRICE and QUANTITY. The program asks for items one by one (a maximum of 10000, tested with `count < MAXITEMS`) and writes each to the file `INVENTORY` with `fprintf()`. The file is then closed and opened again for reading; `fscanf()` reads item by item until it stops returning 4. A table is shown, with value = price × quantity for every item.

```c
#include <stdio.h>
#define MAXITEMS 10000

int main(void)
{
    FILE *fp;
    char name[30], more = 'y';
    int number, qty, count = 0;
    float price;

    fp = fopen("INVENTORY", "w");
    if (fp == NULL) {
        printf("Cannot open INVENTORY\n");
        return 1;
    }
    while (more == 'y' && count < MAXITEMS) {
        printf("Item name, number, price, quantity: ");
        scanf("%29s %d %f %d", name, &number, &price, &qty);
        fprintf(fp, "%s %d %.2f %d\n", name, number, price, qty);
        count++;
        printf("More items (y/n)? ");
        scanf(" %c", &more);
    }
    fclose(fp);

    fp = fopen("INVENTORY", "r");
    if (fp == NULL) {
        printf("Cannot open INVENTORY\n");
        return 1;
    }
    printf("\n%-12s %-8s %-10s %-8s %s\n", "ITEM_NAME", "NUMBER", "PRICE", "QUANTITY", "VALUE");
    while (fscanf(fp, "%29s %d %f %d", name, &number, &price, &qty) == 4)
        printf("%-12s %-8d %-10.2f %-8d %.2f\n", name, number, price, qty, price * qty);
    fclose(fp);
    return 0;
}
```

**Sample run**

Input (entered one after another): `Pen 101 10.50 100 y Notebook 102 45 60 y Bag 103 1200 5 n`

```text
ITEM_NAME    NUMBER   PRICE      QUANTITY VALUE
Pen          101      10.50      100      1050.00
Notebook     102      45.00      60       2700.00
Bag          103      1200.00    5        6000.00
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How does the program make sure no more than 10000 items are stored?
   A. It declares an array of 10000 items
   B. The loop condition includes count < MAXITEMS
   C. It uses fclose() after 10000 items automatically
   D. fopen() refuses more than 10000 lines
   Answer: B. The while loop continues only while more == 'y' and count < MAXITEMS, so entry stops at 10000. (Key point: Limit of 10000 items)

2. Which condition stops the second loop that reads INVENTORY?
   A. fscanf() returns a value other than 4
   B. The user types 'n'
   C. count reaches MAXITEMS
   D. fscanf() returns 0 on the first item
   Answer: A. Each record has four fields; fscanf() returns 4 while a full record is read and fails to return 4 at end of file. (Key point: Reading until end of data)

3. How is the value of each item calculated in the table?
   A. price + quantity
   B. price * quantity
   C. number * price
   D. price / quantity
   Answer: B. Value of an item is its unit price multiplied by the quantity in stock; e.g. Pen 10.50 x 100 = 1050.00. (Key point: Value of each item)

## Related in Computer Programming

- [Display records sorted by name from file](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-sorted-records-by-name) — Computer Programming, chapter 10
- [Store 48 students in file with append](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-48-students-append) — Computer Programming, chapter 10
- [RECORD.txt item cost with VAT](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-record-txt-invoice) — Computer Programming, chapter 10
- [Read records until end; show nth record](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-worker-nth-record) — Computer Programming, chapter 10, asked 2×
- [Employee text file; sort into binary file](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-employee-sorted-binary) — Computer Programming, chapter 10
- [Copy contents of one file to another](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-copy-contents) — Computer Programming, chapter 10, asked 2×

## Related in other subjects

- [Inventory planning and ABC classes from data](https://www.bctengineer.com/question.md?key=semester_8%2Fconstruction_management%3Aabc-table-inventory-2077) — Construction Management, chapter 3
- [File stream; student binary file program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afile-stream-student-binary) — Object Oriented Programming, chapter 8
- [Student file read/write and modify by roll](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-file-modify-roll) — Object Oriented Programming, chapter 8
- [8086 ALP: multiplication table of a number](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-multiplication-table) — Microprocessor, chapter 3
