Professional Documents
Culture Documents
Tutorial 4 Text Files II
Tutorial 4 Text Files II
1.
(a) In the ABC company payroll system, an employee record consists of the following fields:
Declare a structure for the above employee record. Then, declare an array of 1000 employee records.
Using a loop, assign the array with appropriate values (suggestion: 0 for numbers, null otherwise).(10m)
(b) A text file, employee.txt, contains the employee records in the following format:
(i) Declare an input file handle called inPtr, which is a pointer to a FILE type. (2m)
(ii) By using inPtr, open the file employee.txt for reading. (2m)
(iii) Write statements to check whether the file employee.txt has been opened successfully. If not,
print an error message and exit the program. (3m)
(iv) Write statements that will read from the file employee.txt and store the details into the
array of structures declared in part(a). (8m)
2. Assume that a data file called “product.txt” contains the following records.
100 50.00 55.00 40
101 25.00 28.00 95
102 36.00 39.00 25
(iv) Associate the file pointer inPtr (defined in (iii)) to the data file “product.txt”. Open the data file
for reading only.
(2m)
(v) Display an error message in the event that the data file cannot be opened. (1m)
(vi) Read all records from “product.txt” and increase the selling price by 10%. Display each product’s
information and new selling price on the screen. Use the variable newProd (defined in (ii)) as
buffer.
(6m)
(vii) Close the data file. (1m)
-------------------------------------------------------------------------------------------------------------------------------------
Page 1 of 2
Tutorial 4 Text Files II AACS1084 PCDII
3. Consider the following text file “marks.txt” that stores students’ names and subject marks as follows:
(a) Write C statements to declare a FILE pointer filePtr1, and open the file above for reading. (2m)
(b) Declare another FILE pointer filePtr2, and open the file “report.txt” for writing. (2m)
(c) Write C statements to check for file opening errors. (2m)
(d) Write C statements to read the whole “marks.txt” file and calculate the average mark for each of the
students. Send the result of the calculations with proper heading (NAME, MARKS, and
AVERAGE) to the file report.txt. Also, display on screen the students with average less than
50.00%.
(10m)
4. A payroll system uses a text file named PartTime.txt to store the payroll number, the hourly rate, and the
number of hours worked for part-time workers as follows:
Write a program to read the data from PartTime.txt into a structure variable and display the amount of
wages for each of the part-timers, where
Your program should display an error message and terminate the program if the file PartTime.txt cannot be
opened. (15m)
Payroll number Wage (RM)
A sample of the output is as follows: -------------- ---------
2415 112.50
3178 120.00
-------------------------------------------------------------------------------------------------------------------------------------
Page 2 of 2