C PROGRAMMING
POINTERS TO EXTERNAL FILES
STORAGE
The storage of data
in variables and arrays is temporary.
Data is lost when
the program terminates.
Files are used for
permanent retention of large amounts of data.
Files are stored
on secondary devices.
Definitions
Bit - Smallest data item a computer can handle.
Byte
- a pattern of bits used to represent the set of all characters that may
be used within the computer (8 Bits).
Fields
- a group of characters that convey meaning.
Record (struct)
- is a collection of related fields.
File
- is a group of related records.
Record key
- a special field in a record that is used to uniquely identify that record
from all others.
Files
in C
C views each file
simply as a sequential stream of bytes.
Each file ends
with an end-of-file marker.
When a file is
opened a stream is associated with the file.
Three files (streams)
are automatically opened whenever a program is run:
standard input
standard output
standard error
FILES
Opening a file
returns a pointer to a FILE structure (defined in
Opening a file
returns a pointer to a FILE structure (defined in
stdio.h).
standard input
= pointer stdin
standard output
= pointer stdout
standard error
= pointer stderr
The standard
library provides many functions for reading data from files and for writing
data to files.
Functions that
work with files:
fgetc(input stream)
- reads one character from the input stream.
fputc(input stream)
- writes one character to the input stream.
fgets and
fputs
- are used to read and write strings to and from the input stream.
fscanf and
fprintf
- are the corresponding file commands to scanf and printf.
Creating a Sequential
Access File:
C imposes no
structure on a file.
The programmer
must provide the file structure to meet the requirements of his/her application.
To do this the
programmer must understand the data that must be processed.
Use a predetermined
format or design the storage structure from scratch.
Identifying the
file stream:
Before a file
can be used within a C program, a pointer variable must be created to refer
to the file stream.
The FILE type
is used to declare a file pointer variable.
FILE *Myfileptr;
This declaration
identifies a pointer which refers to a file stream.
Each file stream
that will be used must have a unique pointer defined.
Opening a file
stream
Before a file
can be used with in a C program it must be opened for the type of operation
that will be performed.
This is accomplished
by using the fopen command.
Opening a file
fopen(“filename”,”mode”);
Filename can be
represented by either a string constant or a variable.
fopen(“a:\\myfile.dat”,”w”);
filename = “a:\\myfile.data”;
fopen(filename,”w”);
mode can be represented
by a “r” or by a “w”.
fopen(“a:\\myfile.dat”,”w”);
filename = “a:\\myfile.data”;
fopen(filename,”r”);
The “w” indicates
write mode and the “r” indicates read mode.
Read & Write
If you
try to open a file in write mode and the file doesn’t exist a new file
will be created.
If the file does
exist it will be opened and any contents of the file will be discarded
without warning.
If you try to
open a file in read mode and it doesn’t exist you will receive an error
message.
Error checking
When opening
files it is wise to check for errors.
The following
code snippet will provide error checking.
If ((myptr = fopen(“myfile.dat”,”r”) == NULL)
printf(“Error opening file!!”);
else
/* Continue doing whatever you wanted */
Operations on
files
Reading from
a file (opened with the “r”).
fscanf(Myfileptr,”%s”, first_string);
fgets(first_string,10, Myfileptr);
fscanf(Myfileptr,”%d”, &A_Number);
Writing to a
file (opened with the “w”).
fprintf(Myfileptr,”%s\n”,first_string);
fputs(first_string,
Myfileptr);
fprintf(Myfileptr,”%d\n”, A_Number);
File Operations
A file can be
opened for only one operation at a time.
You cannot write
and read from a file at the same time.
You must close
the file and reopen it in the mode you wish to operate.
fclose(filename);
fclose(Myfileptr);
End of File Marker
All files in
C end with and End-of-File marker or what is called an
All files in
C end with and End-of-File marker or what is called an
<EOF>.
This marker is
automatically placed after the last element of data within the file by
the operating system.
You can use this
marker to determine when you have reached the end of a file.
|