SlideShare a Scribd company logo
What is file?
Operation on a file.
Function using in file handling.
Function with example using in file handling.
INTRODUCTIO
N PART
What is file?
A file is a space on the disk where a group of related data is
stored.
Operations on a file
DEFINING A FILE
OPENING A FILE
CLOSING A FILE
WRITING DATA TO FILE
READING DATA FROM A FILE
Functions that helps to perform basic file operations.
fopen( ) - create a new file or open a existing file
fclose( ) - closes a file
fprintf( ) - writes a set of data to a file
fscanf( ) - reads a set of data from a file
fgetc( ) - reads a character from a file
fputc( ) - writes a character to a file
getw( ) - reads a integer from a file
putw( ) - writes a integer to a file
fseek( ) - set the position to desire point
ftell( ) - gives current position in the file
Defining a File
A file is identified by its name. This name is divided into two parts
File Name
 It consists of alphabets and digits.
 Special characters are also supported, but it depends on the operating
system we use.
Extension
 It describes the file type
Opening a File
If we want to store data in file in secondary memory then ,
following things are needed:
*Filename: string of characters to make valid name for o/s.
*Data structure : for a file it is defined as a FILE in <stdio.h>
*Purpose: what to do ; like reading ,writing , append
Syntax:-
FILE *fp;
fp=fopen(“filename”, “mode”);
modes
r
w
a
Open the file in reading mode
Open the file in writing mode
Open the file for adding data to
it
FILE *p1,*p2;
p1=fopen(“data” , “r”);
p2=fopen(“results” , “w”);
Other modes
• The existing file is opened to the
beginning for both reading and writing.
r+
• Same as w except both foe reading
and writing.
w+
• Same as a but both for reading and
writing.
a+
Closing a File
A file is closed when all operations on it have been completed.
It ensures that all the data related to file are cleared from the
buffer.
Example:
FILE *p1,*p2;
p1=fopen(“INPUT” , ”w”);
p2=fopen(“OUTPUT” , ”r”);
…………………
…………………
fclose(p1);
fclose(p2);
fprintf()
&
fscanf()
C library function - fprintf( )
Description:-
The C library function sends formatted output to a stream.
Declaration:-
int fprintf(FILE *stream, const char *format, ...)
Return Value:-
If successful, the total number of characters written is returned
otherwise, a negative number is returned.
#include <stdio.h>
#include <conio.h>
void main(){
FILE * fp;
int c;
fp = fopen ("file.txt", “a+");
fprintf(fp, "%s %s %s %d", "We", "are", "in", 2012);
fclose(fp);
fp = fopen("file.txt","r");
while(1){
c = fgetc(fp);
if( feof(fp) ) {
break;}
printf("%c", c);}
fclose(fp);
getch();
}
file.txt
created on
pc
Example using fprintf() function:-
C library function - fscanf( )
Description:-
The C library function reads formatted input from a stream.
Declaration:-
int fscanf(FILE *stream, const char *format, ...)
#include <stdio.h>
#include <conio.h>
void main()
{
char str1[10], str2[10], str3[10];
int year;
FILE * fp;
fp = fopen ("file.txt", "w+");
fputs(“tWe are in 2012n", fp);
rewind(fp);
fscanf(fp, "%s %s %s %dn", str1, str2, str3, &year);
printf("Read String1 |%s|nRead String2 |%s|nRead String3 |%s|nRead Integer |%d|n", str1,str2,str3,year );
fprintf(fp , “nRead String1 |%s|nRead String2 |%s|nRead String3 |%s|nRead Integer |%d|n", str1,str2,str3,year );
fclose(fp);
getch();
}
Example using fscanf() function:-
fgetc()
&
fputc()
• fgetc() function is used to read a file line by
line.
• It is used in program as:-
ch=fgetc(fs);
• It returns the character that is read, which
is collected in the variable ‘ch’.
C library function - fgetc( )
C library function - fputc( )
• fgetc() is a function which writes
characters to a file.
• It is used in program as:-
fputc(ch,ft);
• With the help of both functions, we can copy contents
of one file to another.
Example using fgetc() & fputc() function:-
int main()
{
FILE *fs,*ft;
char ch;
fs=fopen(“PR1.C”,”r”);
if(fs==NULL)
{
printf(“Cannot open ssource file”);
exit(1);
}
ft=fopen(“PR2.C,”w”);
if(ft==NULL)
{
printf(“Cannot open the file”);
fclose(fs);
exit(2);
}
while(1)
{
ch=fgetc(fs);
if(ch==EOF)
break;
else
fputc(ch,ft);
}
fclose(fs);
fclose(ft);
return 0;
}
putw()
&
getw()
C library function - putw( )
putw() function in “C” programming is use to write
integer data on the file (text file) .
SYNTAX :-
int putw(integer , FILE*);
Example using putw() function:-
Void main()
{
FILE *fp;
fp = fopen(“file1.txt”, “w”);
putw(65,fp);
fclose(fp);
}
C library function - getw( )
 getw() function in “C” programming language is use
to read the integer data from the file.
SYNTAX :-
int getw(FILE*);
Example using getw() function:-
Void main()
{
FILE *fp;
fp = fopen(“file1.txt” , “r” );
printf (“%d” , getw(fp) );
fclose(fp);
}
fseek()
&
ftell()
Random Access of File
Functions Include:-
1. fseek() = to jump a particular location in a file
2. ftell() = current location in a file
Functions used in random access
fseek(): This function is used to move the pointer to a desired position within a
file.
Syntax:
fseek(FILE *stream, long int offset, int whence)
 Stream − This is the pointer to a FILE object that identifies the stream.
 Offset –This specifies the number of positions (bytes)to be moved from the
location specified by position.
 Whence-This is the position from where offset is added. It is specified by
one of the following constants
Constant Description
SEEK_SET Beginning of file
SEEK_CUR Current position of the file pointer
SEEK_END End of file
#include <stdio.h> OUTPUT
void main (){
FILE *fp; ; = FILE OPEN IN WRITE MODE
int c,l
fp = fopen("file.txt",“w");
fputs("This is Deepika", fp);
fseek( fp, 7, SEEK_SET );
fputs(" C Programming Language", fp);
fclose(fp); =FILE OPEN IN READ MODE
fp = fopen("file.txt","r");
while(1)
{c = fgetc(fp);
if( feof(fp) ){
break;}
printf("%c", c);}
fp = fopen("file.txt", “a");
fseek(fp, 0, SEEK_END); =THE FILE SIZE IS 30BYTES
l = ftell(fp);
printf("nnnnTotal size of file.txt = %d bytes", l);
fprintf(fp,”nnnTotal size of the file=%d bytes”,l);
fclose(fp);
getch();
}
File handling in c

More Related Content

What's hot

File handling in C
File handling in CFile handling in C
File handling in C
Rabin BK
 
File in C language
File in C languageFile in C language
File in C language
Manash Kumar Mondal
 
File handling in c
File  handling in cFile  handling in c
File handling in c
thirumalaikumar3
 
File handling in c
File handling in c File handling in c
File handling in c
Vikash Dhal
 
File handling-c programming language
File handling-c programming languageFile handling-c programming language
File handling-c programming language
thirumalaikumar3
 
File in C Programming
File in C ProgrammingFile in C Programming
File in C Programming
Sonya Akter Rupa
 
Understanding c file handling functions with examples
Understanding c file handling functions with examplesUnderstanding c file handling functions with examples
Understanding c file handling functions with examplesMuhammed Thanveer M
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
Vikram Nandini
 
File handling-c
File handling-cFile handling-c
File handling in C by Faixan
File handling in C by FaixanFile handling in C by Faixan
File handling in C by Faixan
ٖFaiXy :)
 
File handling-dutt
File handling-duttFile handling-dutt
File handling-dutt
Anil Dutt
 
file management in c language
file management in c languagefile management in c language
file management in c language
chintan makwana
 
File Management in C
File Management in CFile Management in C
File Management in C
Munazza-Mah-Jabeen
 
File handling in c
File handling in cFile handling in c
File handling in c
aakanksha s
 
File_Management_in_C
File_Management_in_CFile_Management_in_C
File_Management_in_C
NabeelaNousheen
 
Files in C
Files in CFiles in C
Files in C
Prabu U
 
file
filefile
file
teach4uin
 
File Handling in C
File Handling in C File Handling in C
File Handling in C
Subhanshu Maurya
 

What's hot (20)

File handling in C
File handling in CFile handling in C
File handling in C
 
File in C language
File in C languageFile in C language
File in C language
 
File handling in c
File  handling in cFile  handling in c
File handling in c
 
File handling in c
File handling in c File handling in c
File handling in c
 
File handling-c programming language
File handling-c programming languageFile handling-c programming language
File handling-c programming language
 
File in C Programming
File in C ProgrammingFile in C Programming
File in C Programming
 
Understanding c file handling functions with examples
Understanding c file handling functions with examplesUnderstanding c file handling functions with examples
Understanding c file handling functions with examples
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
 
File handling in c
File handling in cFile handling in c
File handling in c
 
File handling-c
File handling-cFile handling-c
File handling-c
 
File handling in C by Faixan
File handling in C by FaixanFile handling in C by Faixan
File handling in C by Faixan
 
File handling-dutt
File handling-duttFile handling-dutt
File handling-dutt
 
File handling in c
File handling in cFile handling in c
File handling in c
 
file management in c language
file management in c languagefile management in c language
file management in c language
 
File Management in C
File Management in CFile Management in C
File Management in C
 
File handling in c
File handling in cFile handling in c
File handling in c
 
File_Management_in_C
File_Management_in_CFile_Management_in_C
File_Management_in_C
 
Files in C
Files in CFiles in C
Files in C
 
file
filefile
file
 
File Handling in C
File Handling in C File Handling in C
File Handling in C
 

Similar to File handling in c

Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdfAdvance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
sangeeta borde
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10patcha535
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
Sandeepbhuma1
 
File management
File managementFile management
File management
lalithambiga kamaraj
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
yuvrajkeshri
 
Unit 5 dwqb ans
Unit 5 dwqb ansUnit 5 dwqb ans
Unit 5 dwqb ans
Sowri Rajan
 
Module 5 file cp
Module 5 file cpModule 5 file cp
Module 5 file cp
Amarjith C K
 
Lecture 20 - File Handling
Lecture 20 - File HandlingLecture 20 - File Handling
Lecture 20 - File Handling
Md. Imran Hossain Showrov
 
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
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C Programming
RavindraSalunke3
 
file handling1
file handling1file handling1
file handling1student
 
File handling(some slides only)
File handling(some slides only)File handling(some slides only)
File handling(some slides only)
Kamlesh Nishad
 
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
BhumaNagaPavan
 

Similar to File handling in c (20)

Unit5
Unit5Unit5
Unit5
 
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdfAdvance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
 
14. fiile io
14. fiile io14. fiile io
14. fiile io
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
 
Unit 8
Unit 8Unit 8
Unit 8
 
File management
File managementFile management
File management
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
Unit v
Unit vUnit v
Unit v
 
Unit 5 dwqb ans
Unit 5 dwqb ansUnit 5 dwqb ans
Unit 5 dwqb ans
 
Module 5 file cp
Module 5 file cpModule 5 file cp
Module 5 file cp
 
Lecture 20 - File Handling
Lecture 20 - File HandlingLecture 20 - File Handling
Lecture 20 - File Handling
 
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
 
Chap 12(files)
Chap 12(files)Chap 12(files)
Chap 12(files)
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C Programming
 
Unit5 C
Unit5 C Unit5 C
Unit5 C
 
file handling1
file handling1file handling1
file handling1
 
File handling(some slides only)
File handling(some slides only)File handling(some slides only)
File handling(some slides only)
 
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
 

Recently uploaded

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 geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
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
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
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
 
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
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 

Recently uploaded (20)

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 geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
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...
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
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 ...
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 

File handling in c

  • 1.
  • 2. What is file? Operation on a file. Function using in file handling. Function with example using in file handling.
  • 4. What is file? A file is a space on the disk where a group of related data is stored. Operations on a file DEFINING A FILE OPENING A FILE CLOSING A FILE WRITING DATA TO FILE READING DATA FROM A FILE
  • 5. Functions that helps to perform basic file operations. fopen( ) - create a new file or open a existing file fclose( ) - closes a file fprintf( ) - writes a set of data to a file fscanf( ) - reads a set of data from a file fgetc( ) - reads a character from a file fputc( ) - writes a character to a file getw( ) - reads a integer from a file putw( ) - writes a integer to a file fseek( ) - set the position to desire point ftell( ) - gives current position in the file
  • 6. Defining a File A file is identified by its name. This name is divided into two parts File Name  It consists of alphabets and digits.  Special characters are also supported, but it depends on the operating system we use. Extension  It describes the file type
  • 7. Opening a File If we want to store data in file in secondary memory then , following things are needed: *Filename: string of characters to make valid name for o/s. *Data structure : for a file it is defined as a FILE in <stdio.h> *Purpose: what to do ; like reading ,writing , append
  • 8. Syntax:- FILE *fp; fp=fopen(“filename”, “mode”); modes r w a Open the file in reading mode Open the file in writing mode Open the file for adding data to it
  • 9. FILE *p1,*p2; p1=fopen(“data” , “r”); p2=fopen(“results” , “w”); Other modes • The existing file is opened to the beginning for both reading and writing. r+ • Same as w except both foe reading and writing. w+ • Same as a but both for reading and writing. a+
  • 10. Closing a File A file is closed when all operations on it have been completed. It ensures that all the data related to file are cleared from the buffer. Example: FILE *p1,*p2; p1=fopen(“INPUT” , ”w”); p2=fopen(“OUTPUT” , ”r”); ………………… ………………… fclose(p1); fclose(p2);
  • 12. C library function - fprintf( ) Description:- The C library function sends formatted output to a stream. Declaration:- int fprintf(FILE *stream, const char *format, ...) Return Value:- If successful, the total number of characters written is returned otherwise, a negative number is returned.
  • 13. #include <stdio.h> #include <conio.h> void main(){ FILE * fp; int c; fp = fopen ("file.txt", “a+"); fprintf(fp, "%s %s %s %d", "We", "are", "in", 2012); fclose(fp); fp = fopen("file.txt","r"); while(1){ c = fgetc(fp); if( feof(fp) ) { break;} printf("%c", c);} fclose(fp); getch(); } file.txt created on pc Example using fprintf() function:-
  • 14. C library function - fscanf( ) Description:- The C library function reads formatted input from a stream. Declaration:- int fscanf(FILE *stream, const char *format, ...)
  • 15. #include <stdio.h> #include <conio.h> void main() { char str1[10], str2[10], str3[10]; int year; FILE * fp; fp = fopen ("file.txt", "w+"); fputs(“tWe are in 2012n", fp); rewind(fp); fscanf(fp, "%s %s %s %dn", str1, str2, str3, &year); printf("Read String1 |%s|nRead String2 |%s|nRead String3 |%s|nRead Integer |%d|n", str1,str2,str3,year ); fprintf(fp , “nRead String1 |%s|nRead String2 |%s|nRead String3 |%s|nRead Integer |%d|n", str1,str2,str3,year ); fclose(fp); getch(); } Example using fscanf() function:-
  • 17. • fgetc() function is used to read a file line by line. • It is used in program as:- ch=fgetc(fs); • It returns the character that is read, which is collected in the variable ‘ch’. C library function - fgetc( )
  • 18. C library function - fputc( ) • fgetc() is a function which writes characters to a file. • It is used in program as:- fputc(ch,ft); • With the help of both functions, we can copy contents of one file to another.
  • 19. Example using fgetc() & fputc() function:- int main() { FILE *fs,*ft; char ch; fs=fopen(“PR1.C”,”r”); if(fs==NULL) { printf(“Cannot open ssource file”); exit(1); } ft=fopen(“PR2.C,”w”); if(ft==NULL) { printf(“Cannot open the file”); fclose(fs); exit(2); } while(1) { ch=fgetc(fs); if(ch==EOF) break; else fputc(ch,ft); } fclose(fs); fclose(ft); return 0; }
  • 21. C library function - putw( ) putw() function in “C” programming is use to write integer data on the file (text file) . SYNTAX :- int putw(integer , FILE*);
  • 22. Example using putw() function:- Void main() { FILE *fp; fp = fopen(“file1.txt”, “w”); putw(65,fp); fclose(fp); }
  • 23. C library function - getw( )  getw() function in “C” programming language is use to read the integer data from the file. SYNTAX :- int getw(FILE*);
  • 24. Example using getw() function:- Void main() { FILE *fp; fp = fopen(“file1.txt” , “r” ); printf (“%d” , getw(fp) ); fclose(fp); }
  • 26. Random Access of File Functions Include:- 1. fseek() = to jump a particular location in a file 2. ftell() = current location in a file
  • 27. Functions used in random access fseek(): This function is used to move the pointer to a desired position within a file. Syntax: fseek(FILE *stream, long int offset, int whence)  Stream − This is the pointer to a FILE object that identifies the stream.  Offset –This specifies the number of positions (bytes)to be moved from the location specified by position.  Whence-This is the position from where offset is added. It is specified by one of the following constants Constant Description SEEK_SET Beginning of file SEEK_CUR Current position of the file pointer SEEK_END End of file
  • 28. #include <stdio.h> OUTPUT void main (){ FILE *fp; ; = FILE OPEN IN WRITE MODE int c,l fp = fopen("file.txt",“w"); fputs("This is Deepika", fp); fseek( fp, 7, SEEK_SET ); fputs(" C Programming Language", fp); fclose(fp); =FILE OPEN IN READ MODE fp = fopen("file.txt","r"); while(1) {c = fgetc(fp); if( feof(fp) ){ break;} printf("%c", c);} fp = fopen("file.txt", “a"); fseek(fp, 0, SEEK_END); =THE FILE SIZE IS 30BYTES l = ftell(fp); printf("nnnnTotal size of file.txt = %d bytes", l); fprintf(fp,”nnnTotal size of the file=%d bytes”,l); fclose(fp); getch(); }