INFORMATION & COMPUTER SCIENCE
DEPARTMENT, KFUPM
ICS – 201 SECTION 55 & 56 (992 Semester)
INTRODUCTION TO COMPUTER SCIENCE
LAB
#02 TOPIC: Text file processing
& use of help utility
Instructor: Bashir M. Ghandi
Objective:
Understand how to get help
using the help utility
Recall
how to open, read from, and write to text files.
Using the Help Utility
·
Turbo C/C++ has an extensive help system that can be
accessed through the Help menu or by pressing the F1 key. It includes a
description of all the library functions that are available including example
programs that uses such function.
·
Open the help utility and get help for the following
functions:
printf scanf fprintf,
stdio.h and fopen
Opening Files
·
The system must prepare a file for input or output
before permitting access i.e., the file should be opened for reading, writing or appending by
using the fopen function and a FILE
pointer variable. E.g.
FILE *inpfile,
*outfile ;
inpfile =
fopen (“input.dat”, “r”) ; à opens existing file
for reading
outfile =
fopen (“output.dat”, “w”) ; à
opens or creates a file for writing
outfile = fopen (“output.dat”, “a”) ; àopens
existing file for appending or
creates one if it does
not exists
With the above declarations, we
can access the corresponding files using their pointers.
Note:
·
Opening a file for read operations, “a”,
require the file to exist. If no file exists, an error is returned
·
Opening a file for write operation “w”,
can be for new or existing files. if no file by that name exists, then one is
created; if any file by that exists then the content of that file will erased
(overwritten).
·
Opening a file for append operation “a”, can
also be done with both new and existing files.
For existing file, data is written at the end of the file.t
·
If the fopen function is unable to open the
file, the file pointer that it returns will have a NULL value. The
following statement detects for an unsuccessful attempt to open a file.
if (inpfile == NULL)
printf
(“Cannot open the file\n”) ;
Rewinding files:
·
When you open a file for reading, the reading cursor
will be placed at the beginning of the file.
·
Each time you
read from the file, the cursor moves down.
By the time you read all the data in the file, the cursor will be at the
end of the file.
·
If at this point we you wish to read from the file the
second time, the cursor must be return to the beginning. This is achieved using the rewind function
as in:
rewind(inpfile)
·
When a program has no further use for a file, it should
close the file by using the function fclose as
fclose (inpfile) ;
The fscanf and
fprintf functions
·
These functions are provided to perform the same
operations as the printf and scanf functions on a file.
·
The functions fprintf and fscanf takes an
additional argument, which is the FILE pointer that identifies the file
to which data is to be written or from which the data is to be read.
So, to write the line “I like C ” to the file pointed by outfile, we write
fprintf
(outfile, “I like C\n”) ;
To read the next float value from
a file pointed by inpfile, we write
fscanf (inpfile, “%f”, &x) ;
·
The function feof is used to test if the read
cursor is at the end of file. The single argument to the function is a FILE
pointer. The function returns positive integer value (true) if all of
the data from the specified file has been read, and reurns zero
otherwise. So, the statement. This
function can be used with while loop to read all the data in a file.
while ( ! feof (inpfile) )
{ fscanf(inpfile,
“%d”, &n);
printf (“%d\n”, n) ;
}
1. The EXAMS.DAT , consist of the ID numbers and grades of 10 students in three exams. The program LAB2_1.C reads the data form the file and prints it on the screen with an additional column containing the average of each student.
Download these two files from my home page, study them and test run the program.
2. Modify the program in (1) such that:
(a) The output is sent to a file RESULTS.DAT
(b) An additional row is created containing the average of each exam.
(c) The overall average is computed and printed at the bottom.
3. Modify the program in (2) such that:
(a) It can work for any number of students
(a) It creates two additional output files, GOOD.DAT containing the ID numbers and average grade of those students whose average is greater or equal to the overall average and BAD.DAT whose average is below the overall average. Note: Do not use array.
4. Write a program that reads the two files generated by program (3) above and creates another file MERGED.DAT that contains the content of GOOD.DAT followed by the content of BAD.DAT.
Home Work:
1. A file EMP.DAT, contains data in the form: ID_NO, Rate, Hours, and Deductions, where Rate is the hourly rate, Hours is the hours worked for the month, and Deductions are the amount to be deducted from the salary. Write a program to generate the pay slip for the employees in the following format:
************************************************************************
ID_No: Hours:
Gross Pay: Deductions: Net Pay:
************************************************************************
Note that the output should be sent to a file. Create your own sample data.
2. A file contains an unknown number of float values. Write a program that prints the standard deviation of the data in the file. Note: Standard Deviation =