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

CTSD2_UNIT-5_File Management in C

The document provides an overview of file management in C programming, detailing file operations such as creating, opening, reading, writing, and closing files. It explains the importance of file handling for data storage, reusability, and efficiency, and distinguishes between text and binary files. Additionally, it covers file pointers, various file operations, and access methods including sequential and random access.

Uploaded by

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

CTSD2_UNIT-5_File Management in C

The document provides an overview of file management in C programming, detailing file operations such as creating, opening, reading, writing, and closing files. It explains the importance of file handling for data storage, reusability, and efficiency, and distinguishes between text and binary files. Additionally, it covers file pointers, various file operations, and access methods including sequential and random access.

Uploaded by

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

Computational Thinking for

Structured Design-II(303105151)
Mrs. Praptiba Solanki
Computer Science & Engineering Department (PIT)
CHAPTER-5
FILE MANAGEMENT IN C
Contents

• Basics of Computer Files


• Working on Different File Operations (Read, Write and Append)
• Understanding of Access of Files Sequential Access and Random
Access

Source:- solar school


File Management
• It is the process in which we create, open, read, write, and close operations
on a file.
• C language provides different functions such as fopen(), fwrite(), fread(),
fseek(), fprintf(), etc. to perform input, output, and many different C file
operations in our program.
Why do we need File Handling in C?
• Operations using the C program are done on a prompt/terminal
which is not stored anywhere.
• The output is deleted when the program is closed. But in the
software industry, most programs are written to store the
information fetched from the program.
• Reusability: The data stored in the file can be accessed, updated,
and deleted anywhere and anytime providing high reusability.
• Portability: Without losing any data, files can be transferred to
another in the computer system.
• Efficient: A large amount of input may be required for some
programs. File handling allows you to easily access a part of a file
using few instructions which saves a lot of time and reduces the
chance of errors.
• Storage Capacity: Files allow you to store a large amount of data
Types of Files in C

There are two types:

1. Text Files: A text file contains


data in the form of ASCII
characters and is generally
used to store a stream of
characters. They are
generally stored with .txt file
extension.

2. Binary Files: A binary file


contains data in binary form
(i.e. 0’s and 1’s) instead of
ASCII characters.
File Pointer in C

• It is a reference to a particular position in the opened file.

• It is used in file handling to perform all file operations such as


read, write, close, etc.

• We use the FILE macro to declare the file pointer variable.


The FILE macro is defined inside <stdio.h> header file.

• Syntax:
File Operations in C

1. Creating a new file – fopen() with attributes as “a” or “a+”


or “w” or “w+”

2. Opening an existing file – fopen()

3. Reading from file – fscanf() or fgets()

4. Writing to a file – fprintf() or fputs()

5. Moving to a specific location in a file – fseek(), rewind()

6. Closing a file – fclose()


Open a File in C
• fopen() function is used with the filename or file path along with
the required access modes.

• file_name: name of the file when present in the same directory


as the source file. Otherwise, full path.

• access_mode: Specifies for what operation the file is being


opened.

• If the file is opened successfully, returns a file pointer to it.

• If the file is not opened, then returns NULL. Source:- cofounders town
File opening modes in C

• File opening modes or access modes specify the allowed


operations on the file to be opened. They are passed as an
argument to the fopen() function.

Source:- Britannica
File opening modes in C

• File opening modes or access modes specify the allowed


operations on the file to be opened. They are passed as an
argument to the fopen() function.

Source:- Britannica
File opening modes in C

• As given above, if you want to perform operations on a binary


file, then you have to append ‘b’ at the last.
• For example, instead of “w”, you have to use “wb”, instead of
“a+” you have to use “a+b”.

Source:- Britannica
File opening in C(Example)

Source:- Britannica
File opening in C(Example)

OUTPUT:

Source:- Britannica
Create a File in C
• The fopen() function can not only open a file but also can create
a file if it does not exist already. For that, we have to use the
modes that allow the creation of a file if not found such as w, w+,
wb, wb+, a, a+, ab, and ab+.

Source:- Britannica
Create a File in C

Source:- Britannica
Create a File in C

OUTPUT:

Source:- Britannica
Reading From a File

• File Read operation is perform using functions fscanf() or fgets().


• Both the functions performed the same operations as that of
scanf and gets but with an additional parameter, the file pointer.
There are also other functions we can use to read from a file.
Such functions are listed below:
Reading From a File
Reading From a File
Example:

• The getc() and some other file reading functions return EOF
(End Of File) when they reach the end of the file while
reading. EOF indicates the end of the file and its value is
implementation-defined.
Write to a File
• The file write operations can be performed by the functions
fprintf() and fputs() with similarities to read operations.
• C programming also provides some other functions that can be
used to write data to a file such as:

Source:- medium
Write to a File

Source:- medium
Write to a File

Example:

Source:- medium
Closing a File

• The fclose() function is used to close the file.


• After successful file operations, you must always close a file to
remove it from the memory.

• Syntax:

• where the file_pointer is the pointer to the opened file.

Source:- medium
Closing a File

Example:

Source:- medium
Read and Write in a Binary File

Opening a Binary File:

• To open a file in binary mode, we use the rb, rb+, ab, ab+,
wb, and wb+ access mode in the fopen() function.

• We also use the .bin file extension in the binary filename.

• Example:
Read and Write in a Binary File

Write to a Binary File:

• We use fwrite() function to write data to a binary file. The


data is written to the binary file in the from of bits (0’s and
1’s).

• Syntax:
Read and Write in a Binary File

Parameters:

• ptr: pointer to the block of memory to be written.

• size: size of each element to be written (in bytes).

• nmemb: number of elements.

• file_pointer: FILE pointer to the output file stream.

Return Value:
• Number of objects written.
Reading from Binary File
• The fread() function can be used to read data from a binary file in
C. The data is read from the file in the same form as it is stored i.e.
binary form.

• Syntax of fread():
Reading from Binary File
Parameters:
• ptr: pointer to the block of memory to read.

• size: the size of each element to read(in bytes).

• nmemb: number of elements.

• file_pointer: FILE pointer to the input file stream.

Return Value:
• Number of objects written.
fseek() in C
• If we have multiple records inside a file and need to access
a particular record that is at a specific position, So we need
to loop through all the records before it to get the record.
• Doing this will waste a lot of memory and operational time.
• To reduce memory consumption and operational time we
can use fseek() which provides an easier way to get to the
required data. fseek() function in C seeks the cursor to the
given record in the file.
fseek() in C
• Syntax:
fseek() in C

OUTPUT:
rewind() in C
• The rewind()function is used to bring the file pointer to the
beginning of the file. It can be used in place of fseek() when you
want the file pointer at the start.

• Syntax of rewind()
rewind() in C(Example)
#include<stdio.h>
#include<conio.h>
void main(){
FILE *fp; char c;
fp=fopen("file.txt","r");

while((c=fgetc(fp))!=EOF){
printf("%c",c);
}

rewind(fp);//moves the file pointer at beginning of the file

while((c=fgetc(fp))!=EOF){
printf("%c",c);
}

fclose(fp);
getch();
}
More File Operations
More File Operations
Understanding of Access of Files

There are two type of file access. They are


1. Sequential file access
2. Random file access
Sequential file access

• Data are stored one after another(sequentially) in the case


of text files. So, data are accessed in the same way in
which they are stored.
Sequential file access(Example)
Sequential file access(Example)
Random file access
• Data can be read or modified randomly in data file, since the
information are stored in the form of records.
• By moving file pointer, we can access any needed information
from a file.
• To read last record of a file, we don't need to read through the
entire file. Instead, we can move the file pointer to last record
and access it.
• Random file access is more efficient when compared to
sequential file access.
Random file access(Example)
Random file access(Example)
Random file access(Example)
Random file access(Example)
www.paruluniversity.ac.in

You might also like