0% found this document useful (0 votes)
11 views26 pages

File Handling

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views26 pages

File Handling

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

C File Handling

Why files are needed?

• When a program is terminated, the entire data is lost. Storing in a file


will preserve your data even if the program terminates.
• If you have to enter a large number of data, it will take a lot of time to
enter them all.
• However, if you have a file containing all the data, you can easily
access the contents of the file using a few commands in C.
• You can easily move your data from one computer to another without
any changes.
Types of Files
When dealing with files, there are two types of files you should know about:
Text files
Binary files
1. Text files
Text files are the normal .txt files. Can easily create text files using any simple text editors such as Notepad.
When you open those files, you'll see all the contents within the file as plain text. You can easily edit or delete the
contents.
They take minimum effort to maintain, are easily readable, and provide the least security and takes bigger storage
space.
2. Binary files
Binary files are mostly the .bin files in your computer.
Instead of storing data in plain text, they store it in the binary form (0's and 1's).
They can hold a higher amount of data, are not readable easily, and provides better security than text files.
File Operations

• In C, you can perform four major operations on files, either text or


binary:
• Creating a new file
• Opening an existing file
• Closing a file
• Reading from and writing information to a file
Working with files

• When working with files, you need to declare a pointer of type file.
This declaration is needed for communication between the file and
the program.
• FILE *fptr;
Opening a file - for creation and edit

Opening a file is performed using the fopen() function defined in


the stdio.h header file.
The syntax for opening a file in standard I/O is:
ptr = f
open("fileopen","mode");
For example,
fopen("E:\\cprogram\\newprogram.txt","w");
fopen("E:\\cprogram\\oldprogram.bin","rb");
Opening Modes in Standard I/O

Mode Meaning of Mode During Inexistence of file

If the file does not exist, fopen()


r Open for reading.
returns NULL.

If the file does not exist, fopen()


rb Open for reading in binary mode.
returns NULL.

If the file exists, its contents are


overwritten.
w Open for writing.
If the file does not exist, it will be
created.

If the file exists, its contents are


overwritten.
wb Open for writing in binary mode.
If the file does not exist, it will be
created.
Open for append. If the file does not exist, it will be
a
Data is added to the end of the file. created.

Open for append in binary mode. If the file does not exist, it will be
ab
Data is added to the end of the file. created.

If the file does not exist, fopen() returns


r+ Open for both reading and writing.
NULL.

Open for both reading and writing in If the file does not exist, fopen() returns
rb+
binary mode. NULL.

If the file exists, its contents are


overwritten.
w+ Open for both reading and writing.
If the file does not exist, it will be
created.
If the file exists, its contents
Open for both reading and are overwritten.
wb+
writing in binary mode. If the file does not exist, it
will be created.

Open for both reading and If the file does not exist, it
a+
appending. will be created.

Open for both reading and If the file does not exist, it
ab+
appending in binary mode. will be created.
Closing a File

• The file (both text and binary) should be closed after reading/writing.
• Closing a file is performed using the fclose() function.
• fclose(fptr);
• fptr is a file pointer associated with the file to be closed.
Reading and writing to a text file

• For reading and writing to a text file, we use the


functions fprintf() and fscanf().
• They are just the file versions of printf() and scanf(). The only
difference is that fprint() and fscanf() expects a pointer to the
structure FILE.
Reading and writing to a binary file

• Functions fread() and fwrite() are used for reading from and writing to a
file on the disk respectively in case of binary files.
• Writing to a binary file
• To write into a binary file, you need to use the fwrite() function. The
functions take four arguments:
• address of data to be written in the disk
• size of data to be written in the disk
• number of such type of data
• pointer to the file where you want to write.
• fwrite(addressData, sizeData, numbersData, pointerToFile);
Getting data using fseek()

• If you have many records inside a file and need to access a record at a
specific position, you need to loop through all the records before it to
get the record.
• This will waste a lot of memory and operation time. An easier way to
get to the required data can be achieved using fseek().
• As the name suggests, fseek() seeks the cursor to the given record in
the file.
Syntax of fseek()

• Parameters
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

Different whence in fseek()


Whence Meaning

SEEK_SET Starts the offset from the beginning of the file.

SEEK_END Starts the offset from the end of the file.

SEEK_CUR Starts the offset from the current location of the cursor in the file.
No. Function Description

1 fopen() opens new or existing file

2 fprintf() write data into the file


3 fscanf() reads data from the file
4 fputc() writes a character into the file

5 fgetc() reads a character from file

6 fclose() closes the file


7 fseek() sets the file pointer to given position

8 fputw() writes an integer to file


9 fgetw() reads an integer from file

10 ftell() returns current position


11 rewind() sets the file pointer to the beginning of the file
C rewind() function

• The rewind() function sets the file pointer at the beginning of the
stream. It is useful if you have to use stream many times.
• Syntax:
• void rewind(FILE *stream)
C ftell() function

• The ftell() function returns the current file position of the specified
stream. We can use ftell() function to get the total size of a file after
moving file pointer at the end of file. We can use SEEK_END constant
to move the file pointer at the end of file.
• Syntax:
• long int ftell(FILE *stream)
1. C program to read name
and marks of n number of
students and store them in a
file.
2. C program to read name and
marks of n number of students
from and store them in a file. If
the file previously exits, add the
information to the file.

You might also like