0% found this document useful (0 votes)
11 views

COMP 003 L8 File Handling

The document provides an overview of file handling in C programming, detailing the concepts of logical and physical files, file streams, and the operations involved in file processing. It explains how to declare, open, write to, read from, and close files, as well as the functions used for these operations. Additionally, it covers file modes, checking for end-of-file conditions, and basic file management tasks such as renaming and deleting files.

Uploaded by

riveme9816
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

COMP 003 L8 File Handling

The document provides an overview of file handling in C programming, detailing the concepts of logical and physical files, file streams, and the operations involved in file processing. It explains how to declare, open, write to, read from, and close files, as well as the functions used for these operations. Additionally, it covers file modes, checking for end-of-file conditions, and basic file management tasks such as renaming and deleting files.

Uploaded by

riveme9816
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

File Handling

COMP 003
BSCS 1-3 | Jairus Chrisnie R. Rimon | Lesson 8

FILE - The logical file or stream


- A file represents a sequence of bytes, ● Is the one being controlled by
regardless of it being a text file or binary program flow.
file. - The physical file or disk file
- File is a collection of logically related ● Is the actual file where the values
information. are saved through the file name.
- File is a built-in structure definition that
holds value into a storage device.
- FILE tends to save the values into a disk,
so that the values can be retrieved for
future use.
- When a computer reads a file, it copies
the file from the storage device to memory;
when it writes to a file, it transfers data
from memory to the storage device.

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

Examples: to use. This is accomplished by using a

FILE *fp; variable called a file pointer.

FILE *fp1, *fp2;


Modes for Opening Files
Opening a FILE: fopen() Read (r)
- To perform any operation on a file, the file Open text file in read mode.
must be brought into memory from the - If the file exists, the marker is positioned
storage device (hard disk). at the beginning.
- This process of bringing the copy of the - If the file doesn’t exist, an error is returned.
file from the disk to memory is called
opening the file. Write (w)
- To open the file, associate the stream Open the file in write mode.
name with the file name. - If a file exists, it is erased.
Syntax: - If a file doesn’t exist, it is created.
filePointer = fopen(“filename”, “mode”);
Append (a)

Example: Open the file in append mode.

fp = fopen(“myfile.txt”, “w”); - If a file exists, the marker is positioned at


the end.

filePointer - is the previously created FILE If a file doesn’t exist, it is created.

structure, this is the logical file.


fopen() - returns a pointer to the file which fopen()

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).

period then a 0 to 3 character length as To specify binary mode, append

extension name. “b” to the string.


File Handling
COMP 003
BSCS 1-3 | Jairus Chrisnie R. Rimon | Lesson 8

Closing a FILE: fclose() fputc(); and putc();


- To close the stream or the file pointer that Syntax:
was previously opened, use the fclose() putc(ch, fp);
function. fputc(ch, fp);
- It is always recommended that we close Both these functions write a character in
the file we have opened previously in order the file at the position specified by the
to ensure that the data written to it will be pointer. The value of a variable ch is written
saved. in the file by fp. It uses the file in the text
Syntax: mode.
fclose(file_pointer_variable);
putw();
Examples: This function is an integer oriented
FILE *ccis; function. This is used for printing an integer
ccis = fopen(“text.txt”, “w”); character in a file.
fclose(ccis); Syntax:
putw(x, fp);
Writing to a FILE The integer value of x is written in file by fp
fprintf(); and then, “fp” file pointer automatically
Prints any type of value/variable in the file. moves forward by (4) 2 bytes file pointer
It behaves similar to the console input and prints the next character. It uses binary
function, the only difference, it works with mode.
files.
fprintf() has 3 parameters: puts(); or fputs();
Syntax: This function is used to write strings in the
fprintf(file, pointer, format specifiers, file.
source variable list); Syntax:
fputs(str, fp);
Example:
fprintf(fp, “%s\n”, name); Reading from a FILE
fp is the file pointer. fscanf();
“%s\n” is the format specifier. fscanf() function has 3 parameters.
name is the variable. Syntax:
fscanf(file pointer, format specifications,
&destination variable list);
File Handling
COMP 003
BSCS 1-3 | Jairus Chrisnie R. Rimon | Lesson 8

Examples: memory to be termed as a structure to a


fscanf(fp, “%s\n”, code); file. The data is written beginning at the
fscanf(fp, “%d\n”, &count); location in the file indicated by the file
position pointer.
Checking of File EOF or NULL fwrite() has 4 parameters:
How to check End Of File condition when Syntax:
using scanf()? fwrite(&source variable, size of source
- Use the function eof. variable in bytes, number of data items to
if(eof(fp)) be appended, file pointer);
printf(“Reached end of a file.”);

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.

You might also like