SlideShare a Scribd company logo
PAPER: INTRODUCTION PROGRAMMING LANGUAGE USING C
PAPER ID: 20105
PAPER CODE: BCA 105
DR. VARUN TIWARI
(ASSOCIATE PROFESSOR)
(DEPARTMENT OF COMPUTER SCIENCE)
BOSCO TECHNICAL TRAINING SOCIETY,
DON BOSCO TECHNICAL SCHOOL, OKHLA ROAD , NEW DELHI
C FILE HANDLING
OBJECTIVES
IN THIS CHAPTER YOU WILL LEARN:
1. TO LEARN ABOUT FILE HANDLING.
2. TO UNDERSTAND TYPES OF FILES IN C.
3. TO UNDERSTAND ABOUT MODES OF FILES IN C.
4. TO LEARN ABOUT READ AND WRITE DATA IN FILE.
5. TO LEARN ABOUT HOW TO COPY CONTENT FROM ONE FILE TO ANOTHER FILE IN C.
FILE : OFTEN DATA IS SO LARGE THAT ALL OF IT CANNOT STORED IN MEMORY AND ONLY A LIMITED
AMOUNT OF IT, CAN BE DISPLAYED ON THE SCREEN. ALSO MEMORY IS VOLATILE AND ITS CONTENTS
WOULD BE LOST ONCE THE PROGRAM TERMINATED. SO THAT SUPPOSE IF YOU WANT TO STORE DATA
THAN YOU USE A FILE HANDLING TECHNIQUE TO STORE DATA IN FILE. THERE ARE TWO TYPES OF FILES
(TEXT FILES (.TXT,.C) AND BINARY FILES (.BIN, .DAT FILE)).
THERE ARE DIFFERENT OPERATIONS THAT CAN BE CARRIED OUT ON A FILE. THESE ARE-:
1. CREATE A NEW FILE
2. OPENING AN EXISTING FILE
3. READING DATA FROM FILE
4. WRITING DATA IN TO FILE
5. MOVING TO A SPECIFIC LOCATION IN A FILE(SEEKING)
6. CLOSING A FILE.
FUNCTIONS OF FILE:
Function Description
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 a character from file
fclose() closes the file
fseek() sets the file pointer to given position
TEXT FILE AND BINARY FILE MODE FOR OPENING A FILE:
Mode Description
r opens a text file in read mode
w opens a text file in write mode
a opens a text file in append mode
r+ opens a text file in read and write mode
w+ opens a text file in read and write mode
a+ opens a text file in read and write mode
rb opens a binary file in read mode
wb opens a binary file in write mode
ab opens a binary file in append mode
rb+ opens a binary file in read and write mode
wb+ opens a binary file in read and write mode
ab+ opens a binary file in read and write mode
FILE OPEN FOPEN(), FGETC() :
SYNTAX : FILE *FOPEN( CONST CHAR * FILENAME, CONST CHAR * MODE );
EX-:
#include<stdio.h>
void main( ) {
file *a ; // file pointer create (create a file)
char c ;
a = fopen("struct1.c","r") ; // opening a file via fopen()
while(1) //until the data available in file
{ c = fgetc ( a ) ; // fgetc read character in afile
if ( c == eof ) // check character until end of file
break ;
printf("%c",c) ; // print character }
fclose (a) ; }
FILE OPEN FSCANF() AND FPRINTF() :
SYNTAX : INT FPRINTF(FILE *STREAM, CONST CHAR *FORMAT [, ARGUMENT, ...])
INT FSCANF(FILE *STREAM, CONST CHAR *FORMAT [, ARGUMENT, ...])
EX-:
#include<stdio.h>
void main(){
FILE *a;
a = fopen("file.txt", "w");//opening file
fprintf(a, "Hello how r u.n");//writing data into file
fclose(a);//closing file
}
INT FSCANF(FILE *STREAM, CONST CHAR *FORMAT [, ARGUMENT, ...])
EX-:
#include<stdio.h>
void main(){
FILE *a;
char b[100];//creating char array to store data of file
a = fopen("file.txt", "r");
while(fscanf(a, "%s", b)!=eof()){
printf("%s ", b);
} fclose(fp); }
STORE STUDENT INFO BY USING FILE HANDLING
#include<stdio.h>
#include<conio.h>
struct stud { int sid;
char sname[20]; int sage;
}; void main() {
struct stud s; FILE *a,*b;
a = fopen("stud.txt", "w"); b = fopen("stud.txt", "r");
printf("n Enter student id , student name and sage");
scanf("%d %s %d", &s.sid,s.sname,&s.sage);
fprintf(a,"%d %s %d", s.sid,s.sname, s.sage);
fclose(a); printf("n The Value of Sid , Sname and sage is-:");
do {
fscanf(b,"%d %s %d", s.sid,s.sname, s.sage); printf("%d %s %d",s.sid, s.sname, s.sage); }
while(!eof(b)); getch(); }
FGETC() AND FPUTC()
SYNTAX: CHAR* FGETS(CHAR *S, INT N, FILE *STREAM) , INT FPUTS(CONST CHAR *S, FILE *STREAM)
void main(){ FILE *a;
clrscr(); a=fopen(“abc.txt","w");
fputs("hello how r u",a);
fclose(a); getch(); }
void main(){ FILE *a;
char b[100]; clrscr();
a=fopen(“abc.txt","r");
printf("%s",fgets(b,100,a));
fclose(a); getch();
}
FSEEK() : INT FSEEK(FILE *STREAM, LONG INT OFFSET, INT WHERE)
THERE ARE 3 CONSTANTS USED IN THE FSEEK() FUNCTION FOR WHERE: SEEK_SET, SEEK_CUR AND
SEEK_END.
void main(){
FILE *a;
a = fopen(“abc.txt","w+");
fputs(“this is my first program", a);
fseek( a, 3, SEEK_SET );
fputs(“c program", a);
fclose(a); }
BINARY FILE FWRITE() :
#include<stdio.h>
struct book{ int bid;
char bname[20]; int bisbn; };
void main() {
struct book b; FILE *fp;
fp = fopen("book.dat","wb");
if(fp == NULL){ puts("Cannot open file"); }
printf("n Enter book id, book name, book isbn");
scanf("%d%s%d",&b.bid,b.bname,&b.bisbn);
fwrite(&b,sizeof(b),1,fp);
fclose(fp); }
BINARY FILE FREAD() :
#include<stdio.h>
struct book{ int bid;
char bname[20]; int bisbn; };
void main() {
struct book b; file *fp;
fp=fopen("book.dat","rb");
while(fread(&b,sizeof(b),1,fp)==1)
{
printf("%d %s %dn",b.bid,b.bname,b.bisbn);
}
fclose(fp); getch(); }
THANK YOU

More Related Content

What's hot

Introduction of file handling
Introduction of file handlingIntroduction of file handling
Introduction of file handling
VC Infotech
 
Files and Directories in PHP
Files and Directories in PHPFiles and Directories in PHP
Files and Directories in PHP
Nicole Ryan
 
Filing system in PHP
Filing system in PHPFiling system in PHP
Filing system in PHP
Mudasir Syed
 
File management and handling by prabhakar
File management and handling by prabhakarFile management and handling by prabhakar
File management and handling by prabhakar
PrabhakarPremUpreti
 
PHP file handling
PHP file handling PHP file handling
PHP file handling
wahidullah mudaser
 
Concept of file handling in c
Concept of file handling in cConcept of file handling in c
Concept of file handling in c
MugdhaSharma11
 
Fileinc
FileincFileinc
Fileinc
Karthic Rao
 
Chap 5 php files part 1
Chap 5 php files part 1Chap 5 php files part 1
Chap 5 php files part 1
monikadeshmane
 
Remove Duplicate Files & Manage
Remove Duplicate Files & ManageRemove Duplicate Files & Manage
Remove Duplicate Files & Manage
folderorganizer
 
Top 10 Linux Commands
Top 10 Linux CommandsTop 10 Linux Commands
Top 10 Linux Commands
Jen L.
 
File accessing modes in c
File accessing modes in cFile accessing modes in c
File accessing modes in c
manojmanoj218596
 
Files let you store data on secondary storage such as a hard disk so that you...
Files let you store data on secondary storage such as a hard disk so that you...Files let you store data on secondary storage such as a hard disk so that you...
Files let you store data on secondary storage such as a hard disk so that you...
Bern Jamie
 
php file uploading
php file uploadingphp file uploading
php file uploading
Purushottam Kumar
 
Data file operations in C++ Base
Data file operations in C++ BaseData file operations in C++ Base
Data file operations in C++ Base
arJun_M
 
비윈도우즈 환경의 기술 서적 번역 도구 경험 공유
비윈도우즈 환경의 기술 서적 번역 도구 경험 공유비윈도우즈 환경의 기술 서적 번역 도구 경험 공유
비윈도우즈 환경의 기술 서적 번역 도구 경험 공유
Younggun Kim
 
5 Structure & File.pptx
5 Structure & File.pptx5 Structure & File.pptx
5 Structure & File.pptx
aarockiaabinsAPIICSE
 
R- create a table from a list of files.... before webmining
R- create a table from a list of files.... before webminingR- create a table from a list of files.... before webmining
R- create a table from a list of files.... before webmining
Gabriela Plantie
 

What's hot (17)

Introduction of file handling
Introduction of file handlingIntroduction of file handling
Introduction of file handling
 
Files and Directories in PHP
Files and Directories in PHPFiles and Directories in PHP
Files and Directories in PHP
 
Filing system in PHP
Filing system in PHPFiling system in PHP
Filing system in PHP
 
File management and handling by prabhakar
File management and handling by prabhakarFile management and handling by prabhakar
File management and handling by prabhakar
 
PHP file handling
PHP file handling PHP file handling
PHP file handling
 
Concept of file handling in c
Concept of file handling in cConcept of file handling in c
Concept of file handling in c
 
Fileinc
FileincFileinc
Fileinc
 
Chap 5 php files part 1
Chap 5 php files part 1Chap 5 php files part 1
Chap 5 php files part 1
 
Remove Duplicate Files & Manage
Remove Duplicate Files & ManageRemove Duplicate Files & Manage
Remove Duplicate Files & Manage
 
Top 10 Linux Commands
Top 10 Linux CommandsTop 10 Linux Commands
Top 10 Linux Commands
 
File accessing modes in c
File accessing modes in cFile accessing modes in c
File accessing modes in c
 
Files let you store data on secondary storage such as a hard disk so that you...
Files let you store data on secondary storage such as a hard disk so that you...Files let you store data on secondary storage such as a hard disk so that you...
Files let you store data on secondary storage such as a hard disk so that you...
 
php file uploading
php file uploadingphp file uploading
php file uploading
 
Data file operations in C++ Base
Data file operations in C++ BaseData file operations in C++ Base
Data file operations in C++ Base
 
비윈도우즈 환경의 기술 서적 번역 도구 경험 공유
비윈도우즈 환경의 기술 서적 번역 도구 경험 공유비윈도우즈 환경의 기술 서적 번역 도구 경험 공유
비윈도우즈 환경의 기술 서적 번역 도구 경험 공유
 
5 Structure & File.pptx
5 Structure & File.pptx5 Structure & File.pptx
5 Structure & File.pptx
 
R- create a table from a list of files.... before webmining
R- create a table from a list of files.... before webminingR- create a table from a list of files.... before webmining
R- create a table from a list of files.... before webmining
 

Similar to File Handling in C Programming

file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
yuvrajkeshri
 
C-FILE MANAGEMENT.pptx
C-FILE MANAGEMENT.pptxC-FILE MANAGEMENT.pptx
C-FILE MANAGEMENT.pptx
banu236831
 
File in C language
File in C languageFile in C language
File in C language
Manash Kumar Mondal
 
data file handling
data file handlingdata file handling
data file handling
krishna partiwala
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
Sandeepbhuma1
 
File handling-c
File handling-cFile handling-c
Files_in_C.ppt
Files_in_C.pptFiles_in_C.ppt
Files_in_C.ppt
kasthurimukila
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
DHARUNESHBOOPATHY
 
Unit5
Unit5Unit5
Unit5
mrecedu
 
File management
File managementFile management
File management
lalithambiga kamaraj
 
File Organization
File OrganizationFile Organization
File Organization
RAMPRAKASH REDDY ARAVA
 
File handling in 'C'
File handling in 'C'File handling in 'C'
File handling in 'C'
Gaurav Garg
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
YOGESH SINGH
 
Module 03 File Handling in C
Module 03 File Handling in CModule 03 File Handling in C
Module 03 File Handling in C
Tushar B Kute
 
1file handling
1file handling1file handling
1file handling
Frijo Francis
 
Presentation on files
Presentation on filesPresentation on files
Presentation on files
mallubenal
 
Chpater29 operation-on-file
Chpater29 operation-on-fileChpater29 operation-on-file
Chpater29 operation-on-file
Deepak Singh
 
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfEASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
sudhakargeruganti
 
Data Structure Using C - FILES
Data Structure Using C - FILESData Structure Using C - FILES
Data Structure Using C - FILES
Harish Kamat
 
filehandling.pptx
filehandling.pptxfilehandling.pptx
filehandling.pptx
Aatif jamshed
 

Similar to File Handling in C Programming (20)

file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
C-FILE MANAGEMENT.pptx
C-FILE MANAGEMENT.pptxC-FILE MANAGEMENT.pptx
C-FILE MANAGEMENT.pptx
 
File in C language
File in C languageFile in C language
File in C language
 
data file handling
data file handlingdata file handling
data file handling
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
 
File handling-c
File handling-cFile handling-c
File handling-c
 
Files_in_C.ppt
Files_in_C.pptFiles_in_C.ppt
Files_in_C.ppt
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
Unit5
Unit5Unit5
Unit5
 
File management
File managementFile management
File management
 
File Organization
File OrganizationFile Organization
File Organization
 
File handling in 'C'
File handling in 'C'File handling in 'C'
File handling in 'C'
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
 
Module 03 File Handling in C
Module 03 File Handling in CModule 03 File Handling in C
Module 03 File Handling in C
 
1file handling
1file handling1file handling
1file handling
 
Presentation on files
Presentation on filesPresentation on files
Presentation on files
 
Chpater29 operation-on-file
Chpater29 operation-on-fileChpater29 operation-on-file
Chpater29 operation-on-file
 
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfEASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
 
Data Structure Using C - FILES
Data Structure Using C - FILESData Structure Using C - FILES
Data Structure Using C - FILES
 
filehandling.pptx
filehandling.pptxfilehandling.pptx
filehandling.pptx
 

More from Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)

String Manipulation Function and Header File Functions
String Manipulation Function and Header File FunctionsString Manipulation Function and Header File Functions
C Structure and Union in C
C Structure and Union in CC Structure and Union in C
Preprocessor Directive in C
Preprocessor Directive in CPreprocessor Directive in C
Bit field enum and command line arguments
Bit field enum and command line argumentsBit field enum and command line arguments
Pointers in C and Dynamic Memory Allocation
Pointers in C and Dynamic Memory AllocationPointers in C and Dynamic Memory Allocation
Array in C
Array in CArray in C
C storage class
C storage classC storage class
Function in C Programming
Function in C ProgrammingFunction in C Programming
C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)C Constructs (C Statements & Loop)
C Operators
C OperatorsC Operators
C programming Basics
C programming BasicsC programming Basics
Software Development Skills and SDLC
Software Development Skills and SDLCSoftware Development Skills and SDLC
Mobile commerce
Mobile commerceMobile commerce
E commerce application
E commerce applicationE commerce application
Data normalization
Data normalizationData normalization
Html Form Controls
Html Form ControlsHtml Form Controls
Security issue in e commerce
Security issue in e commerceSecurity issue in e commerce
ER to Relational Mapping
ER to Relational MappingER to Relational Mapping
Entity Relationship Model
Entity Relationship ModelEntity Relationship Model
Database connectivity with data reader by varun tiwari
Database connectivity with data reader by varun tiwariDatabase connectivity with data reader by varun tiwari

More from Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi) (20)

String Manipulation Function and Header File Functions
String Manipulation Function and Header File FunctionsString Manipulation Function and Header File Functions
String Manipulation Function and Header File Functions
 
C Structure and Union in C
C Structure and Union in CC Structure and Union in C
C Structure and Union in C
 
Preprocessor Directive in C
Preprocessor Directive in CPreprocessor Directive in C
Preprocessor Directive in C
 
Bit field enum and command line arguments
Bit field enum and command line argumentsBit field enum and command line arguments
Bit field enum and command line arguments
 
Pointers in C and Dynamic Memory Allocation
Pointers in C and Dynamic Memory AllocationPointers in C and Dynamic Memory Allocation
Pointers in C and Dynamic Memory Allocation
 
Array in C
Array in CArray in C
Array in C
 
C storage class
C storage classC storage class
C storage class
 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
 
C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)
 
C Operators
C OperatorsC Operators
C Operators
 
C programming Basics
C programming BasicsC programming Basics
C programming Basics
 
Software Development Skills and SDLC
Software Development Skills and SDLCSoftware Development Skills and SDLC
Software Development Skills and SDLC
 
Mobile commerce
Mobile commerceMobile commerce
Mobile commerce
 
E commerce application
E commerce applicationE commerce application
E commerce application
 
Data normalization
Data normalizationData normalization
Data normalization
 
Html Form Controls
Html Form ControlsHtml Form Controls
Html Form Controls
 
Security issue in e commerce
Security issue in e commerceSecurity issue in e commerce
Security issue in e commerce
 
ER to Relational Mapping
ER to Relational MappingER to Relational Mapping
ER to Relational Mapping
 
Entity Relationship Model
Entity Relationship ModelEntity Relationship Model
Entity Relationship Model
 
Database connectivity with data reader by varun tiwari
Database connectivity with data reader by varun tiwariDatabase connectivity with data reader by varun tiwari
Database connectivity with data reader by varun tiwari
 

Recently uploaded

Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 

Recently uploaded (20)

Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 

File Handling in C Programming

  • 1. PAPER: INTRODUCTION PROGRAMMING LANGUAGE USING C PAPER ID: 20105 PAPER CODE: BCA 105 DR. VARUN TIWARI (ASSOCIATE PROFESSOR) (DEPARTMENT OF COMPUTER SCIENCE) BOSCO TECHNICAL TRAINING SOCIETY, DON BOSCO TECHNICAL SCHOOL, OKHLA ROAD , NEW DELHI
  • 3. OBJECTIVES IN THIS CHAPTER YOU WILL LEARN: 1. TO LEARN ABOUT FILE HANDLING. 2. TO UNDERSTAND TYPES OF FILES IN C. 3. TO UNDERSTAND ABOUT MODES OF FILES IN C. 4. TO LEARN ABOUT READ AND WRITE DATA IN FILE. 5. TO LEARN ABOUT HOW TO COPY CONTENT FROM ONE FILE TO ANOTHER FILE IN C.
  • 4. FILE : OFTEN DATA IS SO LARGE THAT ALL OF IT CANNOT STORED IN MEMORY AND ONLY A LIMITED AMOUNT OF IT, CAN BE DISPLAYED ON THE SCREEN. ALSO MEMORY IS VOLATILE AND ITS CONTENTS WOULD BE LOST ONCE THE PROGRAM TERMINATED. SO THAT SUPPOSE IF YOU WANT TO STORE DATA THAN YOU USE A FILE HANDLING TECHNIQUE TO STORE DATA IN FILE. THERE ARE TWO TYPES OF FILES (TEXT FILES (.TXT,.C) AND BINARY FILES (.BIN, .DAT FILE)). THERE ARE DIFFERENT OPERATIONS THAT CAN BE CARRIED OUT ON A FILE. THESE ARE-: 1. CREATE A NEW FILE 2. OPENING AN EXISTING FILE 3. READING DATA FROM FILE 4. WRITING DATA IN TO FILE 5. MOVING TO A SPECIFIC LOCATION IN A FILE(SEEKING) 6. CLOSING A FILE.
  • 5. FUNCTIONS OF FILE: Function Description 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 a character from file fclose() closes the file fseek() sets the file pointer to given position
  • 6. TEXT FILE AND BINARY FILE MODE FOR OPENING A FILE: Mode Description r opens a text file in read mode w opens a text file in write mode a opens a text file in append mode r+ opens a text file in read and write mode w+ opens a text file in read and write mode a+ opens a text file in read and write mode rb opens a binary file in read mode wb opens a binary file in write mode ab opens a binary file in append mode rb+ opens a binary file in read and write mode wb+ opens a binary file in read and write mode ab+ opens a binary file in read and write mode
  • 7. FILE OPEN FOPEN(), FGETC() : SYNTAX : FILE *FOPEN( CONST CHAR * FILENAME, CONST CHAR * MODE ); EX-: #include<stdio.h> void main( ) { file *a ; // file pointer create (create a file) char c ; a = fopen("struct1.c","r") ; // opening a file via fopen() while(1) //until the data available in file { c = fgetc ( a ) ; // fgetc read character in afile if ( c == eof ) // check character until end of file break ; printf("%c",c) ; // print character } fclose (a) ; }
  • 8. FILE OPEN FSCANF() AND FPRINTF() : SYNTAX : INT FPRINTF(FILE *STREAM, CONST CHAR *FORMAT [, ARGUMENT, ...]) INT FSCANF(FILE *STREAM, CONST CHAR *FORMAT [, ARGUMENT, ...]) EX-: #include<stdio.h> void main(){ FILE *a; a = fopen("file.txt", "w");//opening file fprintf(a, "Hello how r u.n");//writing data into file fclose(a);//closing file }
  • 9. INT FSCANF(FILE *STREAM, CONST CHAR *FORMAT [, ARGUMENT, ...]) EX-: #include<stdio.h> void main(){ FILE *a; char b[100];//creating char array to store data of file a = fopen("file.txt", "r"); while(fscanf(a, "%s", b)!=eof()){ printf("%s ", b); } fclose(fp); }
  • 10. STORE STUDENT INFO BY USING FILE HANDLING #include<stdio.h> #include<conio.h> struct stud { int sid; char sname[20]; int sage; }; void main() { struct stud s; FILE *a,*b; a = fopen("stud.txt", "w"); b = fopen("stud.txt", "r"); printf("n Enter student id , student name and sage"); scanf("%d %s %d", &s.sid,s.sname,&s.sage); fprintf(a,"%d %s %d", s.sid,s.sname, s.sage); fclose(a); printf("n The Value of Sid , Sname and sage is-:"); do { fscanf(b,"%d %s %d", s.sid,s.sname, s.sage); printf("%d %s %d",s.sid, s.sname, s.sage); } while(!eof(b)); getch(); }
  • 11. FGETC() AND FPUTC() SYNTAX: CHAR* FGETS(CHAR *S, INT N, FILE *STREAM) , INT FPUTS(CONST CHAR *S, FILE *STREAM) void main(){ FILE *a; clrscr(); a=fopen(“abc.txt","w"); fputs("hello how r u",a); fclose(a); getch(); } void main(){ FILE *a; char b[100]; clrscr(); a=fopen(“abc.txt","r"); printf("%s",fgets(b,100,a)); fclose(a); getch(); }
  • 12. FSEEK() : INT FSEEK(FILE *STREAM, LONG INT OFFSET, INT WHERE) THERE ARE 3 CONSTANTS USED IN THE FSEEK() FUNCTION FOR WHERE: SEEK_SET, SEEK_CUR AND SEEK_END. void main(){ FILE *a; a = fopen(“abc.txt","w+"); fputs(“this is my first program", a); fseek( a, 3, SEEK_SET ); fputs(“c program", a); fclose(a); }
  • 13. BINARY FILE FWRITE() : #include<stdio.h> struct book{ int bid; char bname[20]; int bisbn; }; void main() { struct book b; FILE *fp; fp = fopen("book.dat","wb"); if(fp == NULL){ puts("Cannot open file"); } printf("n Enter book id, book name, book isbn"); scanf("%d%s%d",&b.bid,b.bname,&b.bisbn); fwrite(&b,sizeof(b),1,fp); fclose(fp); }
  • 14. BINARY FILE FREAD() : #include<stdio.h> struct book{ int bid; char bname[20]; int bisbn; }; void main() { struct book b; file *fp; fp=fopen("book.dat","rb"); while(fread(&b,sizeof(b),1,fp)==1) { printf("%d %s %dn",b.bid,b.bname,b.bisbn); } fclose(fp); getch(); }