Sie sind auf Seite 1von 13

UNIT-3

File Handling
 File Handling - Opening or creating a file, closing a file, File
modes, Reading and writing a text file using getc(), putc();
Program to demonstrate the same.
 fprintf() ,fscanf(),fgets(), fputs(); Program to demonstrate the
same.
 Difference between append and write mode, Reading and
writing in a binary file; Related program.
 counting lines in a text file, Search in a text file; Program to
demonstrate the same.
 Random file accessing methods- feof(), fseek(), ftell() and
rewind() functions; Sample programs to demonstrate the same.
 Any sample mini Application demonstrating the use of File as
a storage and accessing unit.
Department of CSE, GEHU, DDUN
What is a Computer File ?
• It is necessary to store the data so that it can be
retrieved and displayed later. One way to store
the fetched information in a file.

• A computer file is a resource that stores digital


data or information on a computer device such as
hard disk, magnetic tape or any external electronic
storage device. A file is identified by a file name.
• For ex.
A file could be any of the types such as Text, Audio,
Video or any other format.
Department of CSE, GEHU, DDUN
– A source code written in C program is stored in a
file with .c extension,
– any other text information may be stored with .doc
or .txt extension.
– Any image may be stored in a .bmp or .jpeg or
.png extensions etc.
REQUIREMENT OF A FILE:
• When a program is terminated the entire data
is lost. Storing in a file will preserve our data
even if the program terminates.

Department of CSE, GEHU, DDUN


TYPES OF FILES:

• Text files (.txt): We can easily create text files


using text editor such as notepad.
• Binary file: These are mostly the .bin files in
our computers. Instead of storing data in plain
text, they store it in the binary forms i.e (0’s
and 1’s).

Department of CSE, GEHU, DDUN


FILES OPERATIONS
File handling in C enables us to create, update, read
and delete the files stored on the local file system
through our C program. The following operations
can be performed on a file:
• Creation of a new file
• Opening an existing file
• Reading from a file
• Writing to a file
• Moving to a specific location in a file
• Closing a file

Department of CSE, GEHU, DDUN


FUNCTIONS FOR FILE
HANDLING
• fopen() : opens new or existing file.
• fprintf() : write data into the file.
• fscanf() : reads data from the file.
• fputc() : writes a character into the file.
• fgetc() : reads character from file.
• fclose() : closes the file.
• fseek() : sets the file pointer to given position.
• fputw() : writes an integer to file.
• fgetw() : reads an integer from file.
• ftell() : returns current position.
• rewind() : sets the file pointer to the beginning of the file.

Department of CSE, GEHU, DDUN


Steps involved in opening a file

• Creating a file.
• Opening an existing file.
• Reading from or Writing to a file.
• Closing a file.

Department of CSE, GEHU, DDUN


DEFINING, OPENING AND CLOSING OF
FILES
• File can defined by declaring a pointer of
type FILE.
• For ex.
FILE *ptr;

• The keyword FILE is defined in the header file


<stdio.h>

Department of CSE, GEHU, DDUN


• The FILE is considered as opaque datatype
i.e. its implementation is hidden. Although it is
type of struct typedef as FILE.

• However, the library knows its internally type


and how to use it.

Department of CSE, GEHU, DDUN


Opening of a File
• fopen( ) function can be used to create a new
file or to open an existing file.
– function initializes a variable of the type FILE,
which contains all the information necessary to
control the information stored inside it.
• The syntax:
FILE *fopen( const char *filename, const
char *access_mode );
– where filename is string, used to identify the file.

Department of CSE, GEHU, DDUN


Possible values of access_mode
• access_mode can possess one of the following
values:
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 append mode
r+ Opens a text file for both reading and writing
rb+ Opens a binary file for reading
wb+ Opens a binary file for writing

Department of CSE, GEHU, DDUN


rb : Opens binary file in read mode.
wb : Opens binary file in write mode.
ab : Opens a binary file in append mode.

The fopen function works in the following way:


• Firstly, it searches the file to be opened.
• Then, it loads the file from the disk and place it into the buffer.
The buffer is used to provide efficiency for the read
operations.
• It sets up a character pointer that points to the first character of
the file

Department of CSE, GEHU, DDUN


Closing a File
• Any file that is opened for either for reading or writing should
be closed.
• fclose() function can be used to close a file:
int fclose(FILE *fp);

• The fclose( ) function returns zero on success or returns EOF


(special character generally -1) if end of file is reached or
there is an error.

Department of CSE, GEHU, DDUN

Das könnte Ihnen auch gefallen