Capstone: validate, sort and report an inventory file

This is an invented practice case, not a reconstruction of an unspecified course case. Build a complete C17 inventory-report program in five stages.

Stage 1: choose a record representation and document ownership. inventory.txt contains a count n in 0..1000, followed by exactly n whitespace-separated triples id quantity unit_cents; IDs are unique and in 1..1000000, quantities 0..10000 and prices 0..1000000. Reject missing, extra, malformed or out-of-range tokens, including numeric overflow. A token longer than 63 bytes is invalid.

Stage 2: load an exactly sized dynamic record array, with size/allocation checks; n = 0 is valid.

Stage 3: write a borrowing item_value function and selection-sort records by ascending ID, without parallel-array mismatches. Use integer cents and justify a safe aggregate type.

Stage 4: only after full validation and successful input close, open inventory-report.csv in w mode. Write the header id,quantity,unit_cents,value_cents and one comma-separated record per line. Print OK items=n total_cents=t after successful output close.

Stage 5: on input, memory or output failure print ERROR input, ERROR memory or ERROR output, return nonzero, and release all owned resources. Invalid input must preserve any existing report; an output write failure may leave a partial report.

A bounded token reader with strtoll/ERANGE is supplied in the model so arbitrary integer overflow need not be guessed with scanf. Before revealing it, outline that helper’s three-result contract. Trace the ownership and resources at each stage. Test normal unsorted data, zero records, duplicate IDs, a truncated record, a huge integer token and missing input. For the three records (20,2,150), (5,0,999), (11,3,200), supply exact stdout and report contents.


