This document discusses file management in C programming. It defines a file as an object that stores data, information, settings or commands used with a computer program. There are two types of files - text files containing ASCII codes and binary files containing bytes. The main file operations are creating, opening, closing, reading and writing to files. To access files in a program, the FILE data type and pointer are used along with the fopen() function to open a file with a specified mode like read, write or append. Files must be closed using fclose() after operations are complete. The document provides details on file modes, defining and opening files.
1. FILE MANAGEMENT in C
VIDHYA B
ASSISTANT PROFESSOR
Department of Computer Science with Data Analytics
Sri Ramakrishna College of Arts and Science
Coimbatore - 641 006
Tamil Nadu, India
1
3. INTRODUCTION TO FILE
MANAGEMENT
• Console oriented – use
terminal (keyboard/screen)
• scanf(“%d”,&i) – read data
from keyboard
• printf(“%d”,i) – print data to
monitor
• Suitable for small volumes of
data
• Data lost when program
terminated
• Large data volumes
• E.g. physical experiments
(CERN collider), human
genome, population
records etc.
• Need for flexible
approach to store/retrieve
data
• Concept of files
3
Sri Ramakrishna College of Arts and Science
4. DEFINITION OF FILE
- A file is an object on a computer, stores data, information,
settings, or commands used with a computer program
- Two kinds of files in a system…
(i) Text files (ASCII) – ASCII codes of digits,
alphabets and symbols
(ii) Binary - contains collection of bytes (0’s and 1’s) and
are compiled version of text files.
4
Sri Ramakrishna College of Arts and Science
5. FILE OPERATIONS
Creating a file
Opening a file
Closing a file
Reading a file
Writing in a file
5
Sri Ramakrishna College of Arts and Science
6. DEFINING AND OPENING FILE
To store data file in secondary memory (disc)
must specify to OS
Filename (e.g. sort.c, input.data)
Data structure (e.g. FILE)
Purpose (e.g. reading, writing, appending)
6
Sri Ramakrishna College of Arts and Science
7. DEFINING AND OPENING FILE
String of characters that make up a valid filename for
OS
May contain two parts
Primary
Optional period with extension
Examples: a.out, prog.c, temp, text.out
7
Sri Ramakrishna College of Arts and Science
8. DEFINING A FILE
To have a way to access the files , let the program
keep track of the file being accessed
FILE - initialize an object of the type FILE
*fp - declare the pointer in order to point to the
file, to read from or to write on
fopen ("filename","mode") – file to open, with
file name and mode of opening as argument
8
Sri Ramakrishna College of Arts and Science
FILE *fp;
fp = fopen ("filename","mode");
9. OPENING A FILE
FILE *fp;
fp=fopen("home.txt", "r");
- The above statement will open the file
“home.txt” in read mode
9
Sri Ramakrishna College of Arts and Science
10. CLOSING A FILE
File must be closed as soon as all operations on it
completed
Ensures
All outstanding information associated with file flushed
out from buffers
All links to file broken
Accidental misuse of file prevented
If want to change mode of file, then first close and
open again
10
Sri Ramakrishna College of Arts and Science
11. CLOSING A FILE
Syntax: fclose(file_pointer);
Example:
FILE *p1, *p2;
p1 = fopen(“INPUT.txt”, “r”);
p2 =fopen(“OUTPUT.txt”, “w”);
……..
……..
fclose(p1);
fclose(p2);
pointer can be reused after closing
11
Sri Ramakrishna College of Arts and Science
12. MODES OF FILE OPERATION
12
Sri Ramakrishna College of Arts and Science
Mode Description
r Opens an existing text file for reading purpose
w Opens a text file for writing. If it does not exist,
then a new file is created
a Opens a text file for writing in appending mode.
If it does not exist, then a new file is created
13. MODES OF FILE OPERATION
13
Sri Ramakrishna College of Arts and Science
Mode Description
r+ Opens a text file for both reading and writing
w+ Opens a text file for both reading and writing. It
first truncates the file to zero length if it exists,
otherwise creates a file if it does not exist.
a+ Opens a text file for both reading and writing. It
creates the file if it does not exist. The reading
will start from the beginning but writing can only
be appended.