CTSD2_UNIT-5_File Management in C
CTSD2_UNIT-5_File Management in C
Structured Design-II(303105151)
Mrs. Praptiba Solanki
Computer Science & Engineering Department (PIT)
CHAPTER-5
FILE MANAGEMENT IN C
Contents
• Syntax:
File Operations in C
• If the file is not opened, then returns NULL. Source:- cofounders town
File opening modes in C
Source:- Britannica
File opening modes in C
Source:- Britannica
File opening modes in C
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
• 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
• Syntax:
Source:- medium
Closing a File
Example:
Source:- medium
Read and Write in 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.
• Example:
Read and Write in a Binary File
• Syntax:
Read and Write in a Binary File
Parameters:
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.
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);
}
while((c=fgetc(fp))!=EOF){
printf("%c",c);
}
fclose(fp);
getch();
}
More File Operations
More File Operations
Understanding of Access of Files