SlideShare a Scribd company logo
1 of 21
File Handling in C
Dr.G.Jasmine Beulah
INTRODUCTION
•In programming, we may require some specific input data to be
generated several numbers of times.
•Sometimes, it is not enough to only display the data on the console.
•The data to be displayed may be very large, and only a limited
amount of data can be displayed on the console, and since the
memory is volatile, it is impossible to recover the programmatically
generated data again and again.
•However, if we need to do so, we may store it onto the local file
system which is volatile and can be accessed every time. Here, comes
the need of file handling in C.
File handling in C enables us to create, update, read, and delete the
files stored on the local file system through our C program.
The following operations can be performed on a file.
•Creation of the new file
•Opening an existing file
•Reading from the file
•Writing to the file
•Deleting the file
FUNCTIONS FOR FILE HANDLING
There are many functions in the C library to open, read, write, search and
close the file.
OPENING FILE: FOPEN()
We must open a file before it can be read, write, or update.
The fopen() function is used to open a file. The syntax of the fopen()
is given below.
FILE *fopen( const char * filename, const char * mode );
The fopen() function accepts two parameters:
The file name (string). If the file is stored at some specific location,
then we must mention the path at which the file is stored.
For example, a file name can be like "c://some_folder/some_file.ext".
The mode in which the file is to be opened. It is a string.
Consider the following example which opens a file in write mode.
CLOSING FILE: FCLOSE()
The fclose() function is used to close a file.
The file must be closed after performing all the operations on it.
The syntax of fclose() function is given below:
int fclose( FILE *fp );
C FPRINTF() AND FSCANF()
The fprintf() function is used to write set of characters into file. It
sends formatted output to a stream.
Syntax:
int fprintf(FILE *stream, const char *format [, argument, ...])
READING DATA INTO FILE
FSCANF() FUNCTION
The fscanf() function is used to read set of characters from file.
It reads a word from the file and returns EOF at the end of file.
int fscanf(FILE *stream, const char *format [, argument, ...])
READING DATA FROM A FILE
C File Example: Storing employee information
FPUTC() AND FGETC()
The fputc() function is used to write a single character into file. It
outputs a character to a stream.
Syntax:
int fputc(int c, FILE *stream)
The fgetc() function returns a single character from the file. It gets a
character from the stream. It returns EOF at the end of file.
Syntax:
int fgetc(FILE *stream)
FPUTS() AND FGETS()
The fputs() function writes a line of characters into file. It outputs
string to a stream.
Syntax:
int fputs(const char *s, FILE *stream)
The fgets() function reads a line of characters from file. It gets string
from a stream.
Syntax:
char* fgets(char *s, int n, FILE *stream)
FSEEK() FUNCTION
The fseek() function is used to set the file pointer to the specified
offset.
It is used to write data into file at desired location.
int fseek(FILE *stream, long int offset, int whence)
There are 3 constants used in the fseek() function for whence:
SEEK_SET, SEEK_CUR and SEEK_END.
REWIND() FUNCTION
The rewind() function sets the file pointer at the beginning of the
stream.
It is useful if you have to use stream many times.
void rewind(FILE *stream)
File Handling in C.pptx

More Related Content

Similar to File Handling in C.pptx

Similar to File Handling in C.pptx (20)

File Handling
File HandlingFile Handling
File Handling
 
File Handling
File HandlingFile Handling
File Handling
 
File management
File managementFile management
File management
 
Programming in C Session 4
Programming in C Session 4Programming in C Session 4
Programming in C Session 4
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C Programming
 
File mangement
File mangementFile mangement
File mangement
 
Unit5
Unit5Unit5
Unit5
 
data file handling
data file handlingdata file handling
data file handling
 
Unit 8
Unit 8Unit 8
Unit 8
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handling
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
 
File Handling
File HandlingFile Handling
File Handling
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
 
INput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptxINput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptx
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
 
File_Management_in_C
File_Management_in_CFile_Management_in_C
File_Management_in_C
 
File management in C++
File management in C++File management in C++
File management in C++
 
File Management in C
File Management in CFile Management in C
File Management in C
 
File Organization
File OrganizationFile Organization
File Organization
 
Unit v
Unit vUnit v
Unit v
 

More from DrJasmineBeulahG

More from DrJasmineBeulahG (9)

Constants and Unformatted Input Output Functions.pptx
Constants and Unformatted Input Output Functions.pptxConstants and Unformatted Input Output Functions.pptx
Constants and Unformatted Input Output Functions.pptx
 
Software Testing.pptx
Software Testing.pptxSoftware Testing.pptx
Software Testing.pptx
 
Software Process Model.ppt
Software Process Model.pptSoftware Process Model.ppt
Software Process Model.ppt
 
NumPy.pptx
NumPy.pptxNumPy.pptx
NumPy.pptx
 
Exception Handling in Python
Exception Handling in PythonException Handling in Python
Exception Handling in Python
 
STUDENT DETAILS DATABASE.pptx
STUDENT DETAILS DATABASE.pptxSTUDENT DETAILS DATABASE.pptx
STUDENT DETAILS DATABASE.pptx
 
Selection Sort.pptx
Selection Sort.pptxSelection Sort.pptx
Selection Sort.pptx
 
Structures
StructuresStructures
Structures
 
Arrays
ArraysArrays
Arrays
 

Recently uploaded

Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 

Recently uploaded (20)

Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

File Handling in C.pptx

  • 1. File Handling in C Dr.G.Jasmine Beulah
  • 2. INTRODUCTION •In programming, we may require some specific input data to be generated several numbers of times. •Sometimes, it is not enough to only display the data on the console. •The data to be displayed may be very large, and only a limited amount of data can be displayed on the console, and since the memory is volatile, it is impossible to recover the programmatically generated data again and again. •However, if we need to do so, we may store it onto the local file system which is volatile and can be accessed every time. Here, comes the need of file handling in C.
  • 3. File handling in C enables us to create, update, read, and delete the files stored on the local file system through our C program. The following operations can be performed on a file. •Creation of the new file •Opening an existing file •Reading from the file •Writing to the file •Deleting the file
  • 4. FUNCTIONS FOR FILE HANDLING There are many functions in the C library to open, read, write, search and close the file.
  • 5. OPENING FILE: FOPEN() We must open a file before it can be read, write, or update. The fopen() function is used to open a file. The syntax of the fopen() is given below. FILE *fopen( const char * filename, const char * mode ); The fopen() function accepts two parameters: The file name (string). If the file is stored at some specific location, then we must mention the path at which the file is stored. For example, a file name can be like "c://some_folder/some_file.ext". The mode in which the file is to be opened. It is a string.
  • 6.
  • 7. Consider the following example which opens a file in write mode.
  • 8. CLOSING FILE: FCLOSE() The fclose() function is used to close a file. The file must be closed after performing all the operations on it. The syntax of fclose() function is given below: int fclose( FILE *fp );
  • 9. C FPRINTF() AND FSCANF() The fprintf() function is used to write set of characters into file. It sends formatted output to a stream. Syntax: int fprintf(FILE *stream, const char *format [, argument, ...])
  • 11. FSCANF() FUNCTION The fscanf() function is used to read set of characters from file. It reads a word from the file and returns EOF at the end of file. int fscanf(FILE *stream, const char *format [, argument, ...])
  • 13. C File Example: Storing employee information
  • 14. FPUTC() AND FGETC() The fputc() function is used to write a single character into file. It outputs a character to a stream. Syntax: int fputc(int c, FILE *stream) The fgetc() function returns a single character from the file. It gets a character from the stream. It returns EOF at the end of file. Syntax: int fgetc(FILE *stream)
  • 15.
  • 16. FPUTS() AND FGETS() The fputs() function writes a line of characters into file. It outputs string to a stream. Syntax: int fputs(const char *s, FILE *stream) The fgets() function reads a line of characters from file. It gets string from a stream. Syntax: char* fgets(char *s, int n, FILE *stream)
  • 17.
  • 18. FSEEK() FUNCTION The fseek() function is used to set the file pointer to the specified offset. It is used to write data into file at desired location. int fseek(FILE *stream, long int offset, int whence) There are 3 constants used in the fseek() function for whence: SEEK_SET, SEEK_CUR and SEEK_END.
  • 19.
  • 20. REWIND() FUNCTION The rewind() function sets the file pointer at the beginning of the stream. It is useful if you have to use stream many times. void rewind(FILE *stream)