COMP 003 L8 File Handling
COMP 003 L8 File Handling
COMP 003
BSCS 1-3 | Jairus Chrisnie R. Rimon | Lesson 8
Characteristics of a FILE
file-stream
- Has a name (logically & physically).
- FILE HANDLING in C programming uses
- Must be opened and closed (when the
FILE STREAM as a means of communication
logical file is closed, the values from the
between programs and data files.
logical file will be stored to the physical
- Stream is a logical device that enables
file).
functions to work with a variety of physical
- Can be written to, or read from, or
devices such as the monitor, printer, and
appended to.
secondary storage such as disk drives.
- Streams are device independent, the
File Processing Activities
functions that write to disk files can also
● Declaring a file-stream.
write to the monitor.
● Opening a file-stream.
- The INPUT STREAM extracts the data from
● Writing to a file-stream.
the file and supplies it to the program.
● Reading from a file-stream.
- The OUTPUT STREAM stores the data into
● Closing a file-stream.
the file supplied by the program.
Declaring a FILE
FILE
To declare a file, we use a file pointer:
When dealing with file, we should
- A pointer to information that defines
remember that we are dealing with 2
various things about the file, including the
objects:
name, status, and current position.
File Handling
COMP 003
BSCS 1-3 | Jairus Chrisnie R. Rimon | Lesson 8
- It identifies a specific disk file and is used mode - is a parameter which specifies the
by the stream associated with it to tell purpose of opening a file.
each of the buffered I/O functions where to
perform operations. When opening a file, you must
- Create the stream via a pointer variable specify what you wish to do with. Example,
using the FILE structure: read from the file, write to the file, or both.
Syntax: You may use a number of different
FILE *file_pointer*variable; files in your program. You must specify
when reading or writing which file you wish
is used in all subsequent file operations. To specify that a given file is being
filename - must be a string that provides a opened or created in text mode, append
valid name for the operating system, for “t” to the string (“rt”, “rw”, “w+t”, etc) or
DOS, a 1 to 8 character length followed by a not at all (“r”, “w”, “w+”, etc).
Example:
End of File char name[20] = “Roy”;
While reading from a data file, if we want to fwrite(&name, sizeof(name), 1, fp);
read the complete file till the end of data,
then an end of file marker should be Reading from a FILE (Binary Mode)
checked. fread();
fread() has 4 parameters:
EOF - is a character and every character Syntax:
read from the file is compared against this
fread(&source variable, size of source
character. variable in bytes, number of data items to
be appended, file pointer);
Read/Write function in Standard Library
fgetc
Example:
- Reads one character from a file.
struct record{
fputc
char name[10];
- Write one character to a file.
int age;
fgets
}myfriend;
- Reads a line to a file.
fread(&myfriend, sizeof(struct record), 1,
fputs
fp);
- Write a line to a file.
fscanf/fprintf
Deleting a Filename
- file processing equivalent to scanf and
remove();
printf.
Syntax:
remove(filename);
Writing to a FILE (Binary Mode)
fwrite();
Example:
Transfer a specified number of bytes
remove(“lyn.txt”);
beginning at a specified location in
File Handling
COMP 003
BSCS 1-3 | Jairus Chrisnie R. Rimon | Lesson 8
Renaming a Filename
rename();
Syntax:
rename(filename to be replaced, new file
name);
Example:
rename(“bones.txt”, “woods.txt”);
fseek();
- Sets the file position of the stream to the
given offset.
Syntax:
int fseek(file stream, long int offset, int
whence);
stream - this is the pointer to a FILE object
that identifies the stream.
offset - this is the number of bytes to offset
from whence.
whence - this is the position from where
offset is added. It is specified by one of the
following constants.
fseek() whence/positions
fseek(fp, size, SEEK_CUR);
- Sets the cursor ahead from the
current position by size bytes.
fseek(fp, -size, SEEK_CUR);
- Sets the cursor back from the
current position by size bytes.
fseek(fp, 0, SEEK_END);
- Sets cursor to the end of the file.
fseek(fp, 0, SEEK_SET);
- Sets cursor to the beginning of the
file.