Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Tutorial 4 Text Files II AACS1084 PCDII

1.
(a) In the ABC company payroll system, an employee record consists of the following fields:

Social security number (integer)


Name (string)
Address (string)
Hourly salary rate (float)

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:

SSN Name Address Hourly Rate


1000 Peter 110,Taman-Cantik,KL 40.0
1002 Jane 222,JalanA10,Taman-Indah,PJ 50.0

(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

Write a complete C program to perform the following tasks:


(i) Use typedef to create a structure type to store product information. Call the new type ProdType. Each
product record contains the following fields:

Field Data Type


Product Code Integer
Cost Double
Selling Price Double
Quantity Integer (4m)

(ii) Declare a variable of type ProdType and call it newProd. (1m)

(iii) Declare a file pointer called inPtr. (1m)

(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%.

A sample of the file output in report.txt is as follows:

(10m)

(e) Write the appropriate statement to close the files. (2m)

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:

Payroll No. Hourly Rate Hours Worked


2415 4.5 25
3178 4.0 30

Given the following structure:


typedef struct
{
int payrollNo;
float hourlyRate;
int hoursWorked;
} PartTimer;

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

Wage = hourlyRate * hoursWorked

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

You might also like