File-Handling
File-Handling
PROBLEM SOLVING --
PPS
SUBJECT CODE-
BTPS-101-18
File HANDLING
By Dr.A.Deepa,AP,Chandigarh
Engineering College
Session Objectives
Explain files
By Dr.A.Deepa,AP,Chandigarh
Engineering College
Explain file pointer
By Dr.A.Deepa,AP,Chandigarh
Engineering College
All files are automatically closed when the program, using them,
terminates, normally by main() returning to the operating system
or by a call to exit().
Types of Files:
1. Text File (or) Sequential File
2. Binary File (or) Random File
Text File :
By Dr.A.Deepa,AP,Chandigarh
Engineering College
It is a Sequence of characters. It can be organised into lines terminated by a
newline character.
Binary File :
It is a Sequence of Bytes with one-to-one correspondence to those in the
external device, that is there are no character translations.
Also the number of bytes written (or read) is the same as the number on
the external device.
It do not have any flags to indicate the end of the file. It can be determined
by the size of the file.
Basic file operations
Naming a file
Opening a file
By Dr.A.Deepa,AP,Chandigarh
Engineering College
Reading data from a file
Writing data to a file
Closing a file
Function Operation
Name
fopen() Creates a new file
Opens an existing file
fclose() Closes a file which has been opened
By Dr.A.Deepa,AP,Chandigarh
Engineering College
getc() Reads a character from a file
putc() Writes a character to a file
fprintf() Writes a set of data values to a file
fscanf() Reads a set of data values from a file
getw() Reads an integer from a file
putw() Writes an integer to a file
fseek() Sets the position to a desired point in the file
ftell() Gives the current position in the file(bytes)
rewind() Set the position to the begining of the file
It is a pointer to a structure which contains the
information about the file
FILE *file pointername;
FILE *fp;
By Dr.A.Deepa,AP,Chandigarh
Engineering College
FILE pointername = fopen(“filename”, “file mode”);
fp=fopen(“emp.dat”,”w”);
Mode Name Meaning
w Open a text file for Writing
r Open a text file for Reading
a Append to a text file
rb Open a binary(random) file for reading
wb Create a binary(random) file for Writing
ab Append to a binary(random) file
r+ Open a text file for Read/Write
By Dr.A.Deepa,AP,Chandigarh
Engineering College
w+ Create a text file for Read/Write
a+ Append or create a text file for Read/Write
r+b Open a binary(random) file for read/write
w+b Create a binary(random) file for read/write
a+b Append a binary(random) file for read/write
fclose(filepointer name);
fclose(fp);
Writing a character
The function used for writing characters to a file, that was
previously opened using fopen(), is fputc().
Syntax :
By Dr.A.Deepa,AP,Chandigarh
Engineering College
Reading a character
The fgetc() function is used for reading characters from a file
opened in read mode, using fopen().
Syntax :
String Input / Output
fputs() and fgets(), which write and read character strings to
and from disk file.
By Dr.A.Deepa,AP,Chandigarh
Engineering College
fprintf() and fscanf()
These functions are similar to printf() and scanf() except that
they operate with files.
Syntax
By Dr.A.Deepa,AP,Chandigarh
Engineering College
int fprintf(FILE *fp, const char *control_string, …);
By Dr.A.Deepa,AP,Chandigarh
Engineering College
The prototype for the rewind() is available in stdio.h
Using feof()
When a file is opened for binary input, an integer value
equal to the EOF (End Of File) may be read.
By Dr.A.Deepa,AP,Chandigarh
Engineering College
though the end of file has not reached.
By Dr.A.Deepa,AP,Chandigarh
Engineering College
The current location of the current active pointer can be
found with the help of the ftell() function
Setting Current File Pointer Position
The fseek() function repositions the filepointer by a
specified number of bytes from the start, the current
position or the end of the stream depending upon the
position specified in the fseek() function.
By Dr.A.Deepa,AP,Chandigarh
Engineering College
Here the offset is the number of bytes beyond the file location
given by origin.
Setting Current File Pointer Position
The origin indicates the starting position of the search and
must have the value of either 0 ,1 or 2.
By Dr.A.Deepa,AP,Chandigarh
Engineering College
Origin File Location
SEEK_SET or 0 Beginning of file
By Dr.A.Deepa,AP,Chandigarh
Engineering College
size_t fread(void *buffer, size_t num_bytes, size_t count FILE *fp);
size_t fwrite(const void *buffer, size_t num_bytes, size_t count FILE *fp);
Thank You
By Dr.A.Deepa,AP,Chandigarh
Engineering College