SlideShare a Scribd company logo
Can you C
File Handling
Rohan K.Gajre.
Drop me a line if I can do anything else for you.
rohangajre@gmail.com
why we need file handling?
To store the output of a program as well as
β€’ Reusability: It helps in preserving the data or information generated after
running the program.
β€’ Large storage capacity: Using files, you need not worry about the
problem of storing data in bulk.
β€’ Saves time: Certain programs require a lot of input from the user. You can
easily access any part of the code with the help of certain commands.
β€’ Portability: You can easily transfer the contents of a file from one
computer system to another without having to worry about the loss of data.
C Programming has some in build library
functions for same as following
β€’ fopen():- Creating a new file or Opening an existing file in your system.
β€’ fclose() :-Closing a file
β€’ getc() :-Reading character from a line
β€’ putc() :-Writing characters in a file
β€’ fscanf():-Reading a set of data from a file
β€’ fprintf():-Writing a set of data in a file
β€’ getw():-Reading an integral value from a file
β€’ putw():-Writing an integral value in a file
β€’ fseek():-Setting a desired position in the file
β€’ ftell():-Getting the current position in the file
β€’ rewind():-Setting the position at the beginning point
β€’ Simply we may say that if you want to save or read your C program output
then use file handling.
Steps for creating file handling
1 create file or open file(fopen())
2 select proper mode of file(r,w,a)
3 close file(fclose())
Common mode of file
β€’ r To open a text file
β€’ w To create or open a file in writing mode
β€’ a To open a file in append mode
β€’ r+ We use it to open a text file in both reading and writing mode
β€’ w+ We use it to open a text file in both reading and writing mode
β€’ a+ We use it to open a text file in both reading and writing mode
β€’ rb We use it to open a binary file in reading mode
β€’ wb We use it to open or create a binary file in writing mode
β€’ ab We use it to open a binary file in append mode
β€’ rb+ We use it to open a binary file in both reading and writing mode
β€’ wb+ We use it to open a binary file in both reading and writing mode
β€’ ab+ We use it to open a binary file in both reading and writing mode
1.Creating a file
β€’ First, create your file pointer it as simple as creating a variable this
declaration is needed for communication between file and the program.
Syntax:
FILE *filepointername;
e.g.
FILE *fp;
2.Open a file
β€’ Opening a file is performed using the fopen() defined in stdio.h headfile
Syntax:
Filepointer name=fopen(β€œfilename.txt”,”mode”);
e.g. fp=fopen(β€œdata.txt”,”w”);
3.Close a file
β€’ The file must be closed after reading and writing.use fclose() function with
name of file pointer.
Synatx:
Fclose(filepointer);
e.g.
fclose(*fp);
β€’ Here is a program in C that illustrates the use of file handling.
try it...
//Copy contents of one file to another
main()
{
FILE *wfp,*rfp;
char ch;
clrscr();
rfp = fopen("source","r");
wfp = fopen("target","w");
if(wfp==NULL || rfp==NULL)
{
printf("nUnable to open the file
");
getch();
exit();
}
while(!feof(rfp))
{
ch = fgetc(rfp);
fputc(ch,wfp);
}
printf("nCopied the file ");
fclose(wfp);
fclose(rfp);
getch();
return 0;
}
//WAP to accept 2 no and print result on file
void main() {
FILE *fp;
int num,i,prod;
clrscr();
fp = fopen("table.dat","w+");
printf("nEnter the integer number ");
scanf("%d",&num);
for(i=1;i<=10;i++) {
printf("n%d * %d = %d",num,i,num*i);
fprintf(fp,"%d %d %dn",num,i,num*i); }
rewind(fp);
getch(); clrscr();
printf("nThe table in the file is nnnn");
while(fscanf(fp,"%d %d %d",&num,&i,&prod)!=EOF)
printf("n%d * %d = %d",num,i,prod);
fclose(fp);
getch();
}
//Counting the number of charactrers, words and the lines in the file
main() {
FILE *rfp;
char ch;
int chars=0,words=0,lines=0;
clrscr();
rfp = fopen("source","r");
if(rfp==NULL) {
printf("nUnable to open the file ");
getch();
exit(); }
while(ch!=EOF) {
ch = fgetc(rfp);
if(ch!=' ' && ch!='n')
chars++;
else
words++;
if(ch=='n')
lines++; }
printf("nThe number of characters in the file are %d",chars);
printf("nThe number of words in the file are %d",words);
printf("nThe number of lines in the file are %d",lines);
fclose(rfp);
getch();
return 0;
}
//Reading the contents of the files
main() {
FILE *rfp;
char ch;
clrscr();
rfp = fopen("source","r");
if(rfp==NULL)
{
printf("nUnable to open the file ");
getch();
exit();
}
printf("nThe contents of the file are n");
while(!feof(rfp))
{
ch = fgetc(rfp);
printf("%c",ch);
}
fclose(rfp);
getch();
return 0; }
//Demo of fprintf () function
main()
{
FILE *wfp;
int num1=100,num2 = 200;
clrscr();
wfp = fopen("abc","w");
if(wfp==NULL)
{
printf("nUnable to open the file ");
getch();
}
printf("nThis is the statement on the screen ");
printf("n%d + %d = %d",num1,num2,num1+num2);
fprintf(wfp,"nThis is the statement in the screen");
fprintf(wfp,"n%d + %d = %d",num1,num2,num1+num2);
fclose(wfp);
getch();
return 0;
}
//Demo of fputc() function
main() {
FILE *wfp;
char ch;
clrscr();
wfp = fopen("text","w");
if(wfp==NULL)
{
printf("nUnable to open the file ");
getch(); }
printf("nEnter '$' to stop ");
while(1) {
scanf("%c",&ch);
if(ch=='$')
break;
fputc(ch,wfp);
}
fclose(wfp);
getch();
}
//Writing the table in file & reading from the file
main() {
FILE *fp;
int num,i,prod;
clrscr();
fp = fopen("table","w");
printf("nEnter the integer number ");
fscanf(stdin,"%d",&num);
for(i=1;i<=10;i++)
{
fprintf(stdout,"n%d * %d = %d",num,i,num*i);
fprintf(fp,"n%d * %d = %d",num,i,num*i);
}
getch();
fclose(fp);
clrscr();
fp = fopen("table","r");
printf("nThe contents of the file are n ");
while(fscanf(fp,"%d %d %d",&num,&i,&prod)!=EOF)
printf("n%d * %d = %d",num,i,prod);
getch(); }
//Record operations using the fprintf ()and fscanf () functions
struct student
{
char name[20];
int roll,age;
}S;
main() {
FILE *fp;
struct student S;
char ch;
fp = fopen("Stud.dat","a");
clrscr();
do
{
fprintf(stdout,"nEnter the details of the studemt ");
fscanf(stdin,"%s %d %d",S.name,&S.roll,&S.age);
fprintf(fp,"n%s %d %d",S.name,S.roll,S.age);
printf("nCont ..........");
fflush(stdin);
scanf("%c",&ch);
}while(ch=='y');
clrscr();
fclose(fp);
fp = fopen("Stud.dat","r");
printf("nthe details of the student are n");
while(!feof(fp))
{
fscanf(fp,"%s %d %d",S.name,&S.roll,&S.age);
printf("n%s t%d %dn",S.name,S.roll,S.age);
}
getch();
return 0;
}
//Concatenating two files in the third file
main()
{
char ch,ch1;
FILE *fp1,*fp2,*fp3;
clrscr();
fp1=fopen("file1","r");
fp2=fopen("file2","r");
fp3=fopen("file3","w");
if(fp1==NULL || fp2==NULL)
{
printf("nUnable to open the file ");
getch();
exit();
}
while(ch!=EOF)
{
ch=fgetc(fp1);
fputc(ch,fp3);
}
while(ch1!=EOF)
{
ch1=fgetc(fp2);
fputc(ch1,fp3);
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
printf("n Task done ............. ");
getch();
return 0;
}
Thank You………

More Related Content

What's hot

C++ Tutorial
C++ TutorialC++ Tutorial
C++ Tutorial
freema48
Β 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
Akshay Ithape
Β 
Introduction to programming with c,
Introduction to programming with c,Introduction to programming with c,
Introduction to programming with c,
Hossain Md Shakhawat
Β 
Introduction of c language
Introduction of c languageIntroduction of c language
Introduction of c language
farishah
Β 
Features of c language 1
Features of c language 1Features of c language 1
Features of c language 1
srmohan06
Β 
A brief introduction to C Language
A brief introduction to C LanguageA brief introduction to C Language
A brief introduction to C Language
Mohamed Elsayed
Β 
Sachin kumar ppt on programming in c
Sachin kumar ppt on programming in cSachin kumar ppt on programming in c
Sachin kumar ppt on programming in c
Sachin Kumar
Β 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
Manoj Tyagi
Β 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
MalikaJoya
Β 
The C++ Programming Language
The C++ Programming LanguageThe C++ Programming Language
The C++ Programming Language
Prof Ansari
Β 
introduction to c language
 introduction to c language introduction to c language
introduction to c language
Rai University
Β 
Brief introduction to the c programming language
Brief introduction to the c programming languageBrief introduction to the c programming language
Brief introduction to the c programming languageKumar Gaurav
Β 
C Language
C LanguageC Language
C Language
Aakash Singh
Β 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
MOHAMAD NOH AHMAD
Β 
Computer programming - turbo c environment
Computer programming - turbo c environmentComputer programming - turbo c environment
Computer programming - turbo c environment
John Paul Espino
Β 
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTREC & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
jatin batra
Β 
OOP Poster Presentation
OOP Poster PresentationOOP Poster Presentation
OOP Poster Presentation
Md Mofijul Haque
Β 
What is turbo c and how it works
What is turbo c and how it worksWhat is turbo c and how it works
What is turbo c and how it works
Mark John Lado, MIT
Β 
Structure of C program
Structure of C programStructure of C program
Structure of C program
Pavan prasad
Β 
Unit 2 introduction to c programming
Unit 2   introduction to c programmingUnit 2   introduction to c programming
Unit 2 introduction to c programming
Mithun DSouza
Β 

What's hot (20)

C++ Tutorial
C++ TutorialC++ Tutorial
C++ Tutorial
Β 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
Β 
Introduction to programming with c,
Introduction to programming with c,Introduction to programming with c,
Introduction to programming with c,
Β 
Introduction of c language
Introduction of c languageIntroduction of c language
Introduction of c language
Β 
Features of c language 1
Features of c language 1Features of c language 1
Features of c language 1
Β 
A brief introduction to C Language
A brief introduction to C LanguageA brief introduction to C Language
A brief introduction to C Language
Β 
Sachin kumar ppt on programming in c
Sachin kumar ppt on programming in cSachin kumar ppt on programming in c
Sachin kumar ppt on programming in c
Β 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
Β 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
Β 
The C++ Programming Language
The C++ Programming LanguageThe C++ Programming Language
The C++ Programming Language
Β 
introduction to c language
 introduction to c language introduction to c language
introduction to c language
Β 
Brief introduction to the c programming language
Brief introduction to the c programming languageBrief introduction to the c programming language
Brief introduction to the c programming language
Β 
C Language
C LanguageC Language
C Language
Β 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
Β 
Computer programming - turbo c environment
Computer programming - turbo c environmentComputer programming - turbo c environment
Computer programming - turbo c environment
Β 
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTREC & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
Β 
OOP Poster Presentation
OOP Poster PresentationOOP Poster Presentation
OOP Poster Presentation
Β 
What is turbo c and how it works
What is turbo c and how it worksWhat is turbo c and how it works
What is turbo c and how it works
Β 
Structure of C program
Structure of C programStructure of C program
Structure of C program
Β 
Unit 2 introduction to c programming
Unit 2   introduction to c programmingUnit 2   introduction to c programming
Unit 2 introduction to c programming
Β 

Similar to File handling With Solve Programs

637225560972186380.pdf
637225560972186380.pdf637225560972186380.pdf
637225560972186380.pdf
SureshKalirawna
Β 
File management
File managementFile management
File management
sumathiv9
Β 
Unit5
Unit5Unit5
Unit5mrecedu
Β 
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
Β 
File handing in C
File handing in CFile handing in C
File handing in C
shrishcg
Β 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
yuvrajkeshri
Β 
File handling in C
File handling in CFile handling in C
File handling in C
Kamal Acharya
Β 
File mangement
File mangementFile mangement
File mangement
Jigarthacker
Β 
C-Programming Chapter 5 File-handling-C.ppt
C-Programming Chapter 5 File-handling-C.pptC-Programming Chapter 5 File-handling-C.ppt
C-Programming Chapter 5 File-handling-C.ppt
ssuserad38541
Β 
COM1407: File Processing
COM1407: File Processing COM1407: File Processing
COM1407: File Processing
Hemantha Kulathilake
Β 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
YOGESH SINGH
Β 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
Sandeepbhuma1
Β 
file_c.pdf
file_c.pdffile_c.pdf
file_c.pdf
Osmania University
Β 
file management in c language
file management in c languagefile management in c language
file management in c language
chintan makwana
Β 
File management
File managementFile management
File management
AnishaThakkar2
Β 
1file handling
1file handling1file handling
1file handlingFrijo Francis
Β 
File in c
File in cFile in c
File in c
Prabhu Govind
Β 
File in C Programming
File in C ProgrammingFile in C Programming
File in C Programming
Sonya Akter Rupa
Β 

Similar to File handling With Solve Programs (20)

637225560972186380.pdf
637225560972186380.pdf637225560972186380.pdf
637225560972186380.pdf
Β 
File management
File managementFile management
File management
Β 
Unit5
Unit5Unit5
Unit5
Β 
Introduction to c part 4
Introduction to c  part  4Introduction to c  part  4
Introduction to c part 4
Β 
Module 03 File Handling in C
Module 03 File Handling in CModule 03 File Handling in C
Module 03 File Handling in C
Β 
File handing in C
File handing in CFile handing in C
File handing in C
Β 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
Β 
File handling in C
File handling in CFile handling in C
File handling in C
Β 
File operations in c
File operations in cFile operations in c
File operations in c
Β 
File mangement
File mangementFile mangement
File mangement
Β 
C-Programming Chapter 5 File-handling-C.ppt
C-Programming Chapter 5 File-handling-C.pptC-Programming Chapter 5 File-handling-C.ppt
C-Programming Chapter 5 File-handling-C.ppt
Β 
COM1407: File Processing
COM1407: File Processing COM1407: File Processing
COM1407: File Processing
Β 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
Β 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
Β 
file_c.pdf
file_c.pdffile_c.pdf
file_c.pdf
Β 
file management in c language
file management in c languagefile management in c language
file management in c language
Β 
File management
File managementFile management
File management
Β 
1file handling
1file handling1file handling
1file handling
Β 
File in c
File in cFile in c
File in c
Β 
File in C Programming
File in C ProgrammingFile in C Programming
File in C Programming
Β 

Recently uploaded

A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
Β 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
Β 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
Β 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
Β 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
Β 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
Β 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
Β 
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
Β 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
Β 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
Β 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
Β 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
Β 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
Β 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
Β 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
Β 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
Β 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
DhatriParmar
Β 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
Β 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
Β 
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.
Β 

Recently uploaded (20)

A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
Β 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
Β 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Β 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Β 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
Β 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Β 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Β 
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...
Β 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
Β 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Β 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Β 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
Β 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
Β 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Β 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Β 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
Β 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
Β 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Β 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Β 
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
Β 

File handling With Solve Programs

  • 1. Can you C File Handling Rohan K.Gajre. Drop me a line if I can do anything else for you. rohangajre@gmail.com
  • 2. why we need file handling? To store the output of a program as well as β€’ Reusability: It helps in preserving the data or information generated after running the program. β€’ Large storage capacity: Using files, you need not worry about the problem of storing data in bulk. β€’ Saves time: Certain programs require a lot of input from the user. You can easily access any part of the code with the help of certain commands. β€’ Portability: You can easily transfer the contents of a file from one computer system to another without having to worry about the loss of data. C Programming has some in build library functions for same as following β€’ fopen():- Creating a new file or Opening an existing file in your system. β€’ fclose() :-Closing a file β€’ getc() :-Reading character from a line β€’ putc() :-Writing characters in a file β€’ fscanf():-Reading a set of data from a file
  • 3. β€’ fprintf():-Writing a set of data in a file β€’ getw():-Reading an integral value from a file β€’ putw():-Writing an integral value in a file β€’ fseek():-Setting a desired position in the file β€’ ftell():-Getting the current position in the file β€’ rewind():-Setting the position at the beginning point β€’ Simply we may say that if you want to save or read your C program output then use file handling. Steps for creating file handling 1 create file or open file(fopen()) 2 select proper mode of file(r,w,a) 3 close file(fclose()) Common mode of file β€’ r To open a text file β€’ w To create or open a file in writing mode β€’ a To open a file in append mode β€’ r+ We use it to open a text file in both reading and writing mode β€’ w+ We use it to open a text file in both reading and writing mode
  • 4. β€’ a+ We use it to open a text file in both reading and writing mode β€’ rb We use it to open a binary file in reading mode β€’ wb We use it to open or create a binary file in writing mode β€’ ab We use it to open a binary file in append mode β€’ rb+ We use it to open a binary file in both reading and writing mode β€’ wb+ We use it to open a binary file in both reading and writing mode β€’ ab+ We use it to open a binary file in both reading and writing mode 1.Creating a file β€’ First, create your file pointer it as simple as creating a variable this declaration is needed for communication between file and the program. Syntax: FILE *filepointername; e.g. FILE *fp; 2.Open a file β€’ Opening a file is performed using the fopen() defined in stdio.h headfile Syntax: Filepointer name=fopen(β€œfilename.txt”,”mode”); e.g. fp=fopen(β€œdata.txt”,”w”);
  • 5. 3.Close a file β€’ The file must be closed after reading and writing.use fclose() function with name of file pointer. Synatx: Fclose(filepointer); e.g. fclose(*fp); β€’ Here is a program in C that illustrates the use of file handling. try it...
  • 6. //Copy contents of one file to another main() { FILE *wfp,*rfp; char ch; clrscr(); rfp = fopen("source","r"); wfp = fopen("target","w"); if(wfp==NULL || rfp==NULL) { printf("nUnable to open the file "); getch(); exit(); } while(!feof(rfp)) { ch = fgetc(rfp); fputc(ch,wfp); } printf("nCopied the file "); fclose(wfp); fclose(rfp); getch(); return 0; }
  • 7. //WAP to accept 2 no and print result on file void main() { FILE *fp; int num,i,prod; clrscr(); fp = fopen("table.dat","w+"); printf("nEnter the integer number "); scanf("%d",&num); for(i=1;i<=10;i++) { printf("n%d * %d = %d",num,i,num*i); fprintf(fp,"%d %d %dn",num,i,num*i); } rewind(fp); getch(); clrscr(); printf("nThe table in the file is nnnn"); while(fscanf(fp,"%d %d %d",&num,&i,&prod)!=EOF) printf("n%d * %d = %d",num,i,prod); fclose(fp); getch(); }
  • 8. //Counting the number of charactrers, words and the lines in the file main() { FILE *rfp; char ch; int chars=0,words=0,lines=0; clrscr(); rfp = fopen("source","r"); if(rfp==NULL) { printf("nUnable to open the file "); getch(); exit(); } while(ch!=EOF) { ch = fgetc(rfp); if(ch!=' ' && ch!='n') chars++;
  • 9. else words++; if(ch=='n') lines++; } printf("nThe number of characters in the file are %d",chars); printf("nThe number of words in the file are %d",words); printf("nThe number of lines in the file are %d",lines); fclose(rfp); getch(); return 0; }
  • 10. //Reading the contents of the files main() { FILE *rfp; char ch; clrscr(); rfp = fopen("source","r"); if(rfp==NULL) { printf("nUnable to open the file "); getch(); exit(); } printf("nThe contents of the file are n"); while(!feof(rfp)) { ch = fgetc(rfp); printf("%c",ch); } fclose(rfp); getch(); return 0; }
  • 11. //Demo of fprintf () function main() { FILE *wfp; int num1=100,num2 = 200; clrscr(); wfp = fopen("abc","w"); if(wfp==NULL) { printf("nUnable to open the file "); getch(); } printf("nThis is the statement on the screen "); printf("n%d + %d = %d",num1,num2,num1+num2); fprintf(wfp,"nThis is the statement in the screen"); fprintf(wfp,"n%d + %d = %d",num1,num2,num1+num2); fclose(wfp); getch(); return 0; }
  • 12. //Demo of fputc() function main() { FILE *wfp; char ch; clrscr(); wfp = fopen("text","w"); if(wfp==NULL) { printf("nUnable to open the file "); getch(); } printf("nEnter '$' to stop "); while(1) { scanf("%c",&ch); if(ch=='$') break; fputc(ch,wfp); } fclose(wfp); getch(); }
  • 13. //Writing the table in file & reading from the file main() { FILE *fp; int num,i,prod; clrscr(); fp = fopen("table","w"); printf("nEnter the integer number "); fscanf(stdin,"%d",&num); for(i=1;i<=10;i++) { fprintf(stdout,"n%d * %d = %d",num,i,num*i); fprintf(fp,"n%d * %d = %d",num,i,num*i); } getch(); fclose(fp); clrscr(); fp = fopen("table","r"); printf("nThe contents of the file are n "); while(fscanf(fp,"%d %d %d",&num,&i,&prod)!=EOF) printf("n%d * %d = %d",num,i,prod); getch(); }
  • 14. //Record operations using the fprintf ()and fscanf () functions struct student { char name[20]; int roll,age; }S; main() { FILE *fp; struct student S; char ch; fp = fopen("Stud.dat","a"); clrscr(); do { fprintf(stdout,"nEnter the details of the studemt "); fscanf(stdin,"%s %d %d",S.name,&S.roll,&S.age); fprintf(fp,"n%s %d %d",S.name,S.roll,S.age);
  • 15. printf("nCont .........."); fflush(stdin); scanf("%c",&ch); }while(ch=='y'); clrscr(); fclose(fp); fp = fopen("Stud.dat","r"); printf("nthe details of the student are n"); while(!feof(fp)) { fscanf(fp,"%s %d %d",S.name,&S.roll,&S.age); printf("n%s t%d %dn",S.name,S.roll,S.age); } getch(); return 0; }
  • 16. //Concatenating two files in the third file main() { char ch,ch1; FILE *fp1,*fp2,*fp3; clrscr(); fp1=fopen("file1","r"); fp2=fopen("file2","r"); fp3=fopen("file3","w"); if(fp1==NULL || fp2==NULL) { printf("nUnable to open the file "); getch(); exit(); }