A file inventory.txt stores product stock levels in the format "ProductName,Quantity". A program needs to update the quantity for a specific product. (a) Wr...

Assessment: Computer Science 0478 | Paper 2 Mock 01 | Algorithms, Programming and Logic Subject: Computer Science - 0478

Question 1 Report

A file inventory.txt stores product stock levels in the format "ProductName,Quantity". A program needs to update the quantity for a specific product.

(a) Write pseudocode to update the quantity of a given product. Since text files cannot be modified in-place, you must read the original file, write to a temporary file with the updated record, then replace the original. [6]

(b) Explain why a temporary file is needed for this operation. [2]

(c) State what would happen if the program crashes after writing the temporary file but before replacing the original. [2]

Answer Details

(a) The update algorithm reads every record from the original file, writes each one to a temporary file, and substitutes the updated quantity for the matching product. After processing, the temporary file contains the complete updated data. [6]

DECLARE SearchProduct : STRING
DECLARE NewQuantity : INTEGER
DECLARE Line : STRING
DECLARE ProductName : STRING
DECLARE CommaPos : INTEGER
DECLARE i : INTEGER
DECLARE Found : BOOLEAN
DECLARE UpdatedLine : STRING

OUTPUT "Enter product to update: "
INPUT SearchProduct
OUTPUT "Enter new quantity: "
INPUT NewQuantity

Found ← FALSE
OPENFILE "inventory.txt" FOR READ
OPENFILE "temp.txt" FOR WRITE

WHILE NOT EOF("inventory.txt") DO
    READFILE "inventory.txt", Line
    CommaPos ← 0
    FOR i ← 1 TO LENGTH(Line)
        IF Line[i] = ',' THEN
            CommaPos ← i
        ENDIF
    NEXT i
    ProductName ← SUBSTRING(Line, 1, CommaPos - 1)
    IF ProductName = SearchProduct THEN
        UpdatedLine ← ProductName & "," & NUM_TO_STR(NewQuantity)
        WRITEFILE "temp.txt", UpdatedLine
        Found ← TRUE
    ELSE
        WRITEFILE "temp.txt", Line
    ENDIF
ENDWHILE

CLOSEFILE "inventory.txt"
CLOSEFILE "temp.txt"

IF Found THEN
    OUTPUT "Product updated successfully"
ELSE
    OUTPUT "Product not found"
ENDIF

The algorithm works through these stages:

  1. Input: The user enters the product name to search for and the new quantity value.
  2. File setup: The original file is opened for reading and a temporary file for writing. The Found flag is initialised to FALSE.
  3. Record processing: Each line is read from the original file. A FOR loop scans the line to find the comma position, which separates the product name from the quantity. SUBSTRING extracts the product name (characters from position 1 to one before the comma).
  4. Conditional writing: If the product name matches the search term, a new line is constructed with the original name and the new quantity, and this is written to the temp file. Otherwise, the original line is written unchanged. This ensures every record from the original file appears in the temp file.
  5. Completion: Both files are closed. A message indicates whether the product was found and updated.

(b) A temporary file is needed because text files are sequential-access and do not support modifying individual records in place. [2]

When a text file is opened for reading, data can only be read sequentially from start to end. There is no mechanism to seek to a specific record and overwrite just that record's quantity. If the new quantity has a different number of digits than the old one, the record would be a different length, which would corrupt all subsequent records by shifting their positions. The only reliable approach is to read each record, decide whether to modify it, and write the result to a new file.

(c) If the program crashes after writing the temporary file but before replacing the original: [2]

  1. Both files would exist on disk: "inventory.txt" with the old (pre-update) data and "temp.txt" with the correctly updated data. No data is lost because the original file was only ever read from, never modified.
  2. However, the update is incomplete because the original file still contains the old quantity. The user would need to either run the program again or manually rename "temp.txt" to "inventory.txt" to complete the operation. The presence of both files makes recovery straightforward compared to a scenario where the original was being modified directly and was left in a partially written state.

Download The App On Google Playstore

Everything you need to excel in your exams

Green Bridge CBT Mobile App
Personalized AI Learning Chat Assistant
200,000+ Exam Questions Across IGCSE, JAMB, WAEC & NECO
Over 3,900 Lesson Notes
Offline Support - Learn Anytime, Anywhere
Green Bridge Timetable
Literature Summaries & Potential Questions
Track Your Performance & Progress
In-depth Explanations for Comprehensive Learning