Review & Lab assignments Lab guide # 10 C File Processing Review & Lab assignments
Key points Data hierarchy File & Stream Create, read, write and update: Sequential access file Random access file
Data hierarchy Bit, bytes, characters, records, files database
File & Stream File: Stream: Read/Write function A sequence of bytes, ends with end-of-file marker Structure File descriptor File control block Stream: Provide communication channel between files and programs File pointer: to be returned when open a file stdin, stdout, stderr Read/Write function fgets, fputc, fgets, fputs, fscanf, fprintf
Create, read, write and update FILE *cfPtr; cfPtr = fopen(“clients.dat", “w”); Opening modes Character mode: r,w,a,r+,w+,a+, Binary mode: rb,wb,ab,rb+,wb+,ab+ Functions Random Sequential Read fread fscanf Write fwrite fprintf Pointer position fseek rewind Others feof, fclose
Sequential vs. Random access file Properties Random Sequential Length of records Fixed Dynamic Access individual records without searching through other records Yes No, should be searched through other records Instant access to records No Data can be inserted without destroying other data No, Other data can be destroyed Data previously stored can be updated or deleted without overwriting Storage memory High Low Human readable
Lab Assignment Fig. 11.8 Fig 11.12 Fig 11.15
4. Lab Assignmentstion 11 Hardware Store You are the owner of a hardware store and need to keep an inventory. Tool Record Record # Tool name Quantity Cost The tool identification number should be the record number.
Program Initialize the random-access file “hardware.dat” to 100 empty records. (ONLY if it was empty) Menu Input a tool data. List all tools. Delete a tool record. Update a tool record.
Use this information to start your file. Record number Tool name Quantity Cost 3 Sander 18 35.99 19 Hammer 128 10.00 26 Jigsaw 16 14.25 36 Mower 10 79.50 56 Saw 8 89.99 76 Screwdriver 236 4.99 81 Sledgehammer 32 19.75 88 Wrench 65 6.48