SlideShare a Scribd company logo
1 of 10
File handling in C
File handling in C stores data of our program in our local store, which can be used
at any time because as the execution of a program completes, our data is lost.
Therefore, we need to save our data in any file form - text or binary files.
We can perform tasks like opening, reading the existing file, writing or adding new
content, creating a new file, closing the file, or even saving the file to a certain
location using C.
A few features of using files
•Reusability: Data stored in files can be used
repeatedly.
•Portability: Files enable data transfer between
different systems.
•Efficient: Quick access to stored data.
•Storage Capacity: Large amounts of data can be
stored beyond the constraints of RAM.
Types of Files in C
We will be working with two types of files: -
1.Text file
2.Binary file
Let’s understand them in brief -
Text file - The user can create these files easily while handling files in C.
It stores information in the form of ASCII characters internally, and when
the file is opened, the content is readable by humans. It can be created by
any text editor with a .txt or .rtf (rich text)extension. Since text files are
simple, they can be edited by any text editor like Microsoft Word,
Notepad, Apple Text Edit, etc.
Binary file - It stores information in the form of 0’s or 1’s, and it is saved
with the .bin extension, taking less space. Since it is stored in a binary
number system format, it is not readable by humans. Therefore it is more
secure than a text file.
C File Operations
There are different kinds of file operations in C.
1.Creating a new file
2.Opening an existing file
3.Writing data to a file
4.Reading data from an existing file.
5.Moving data to a specific location on the file
6.Closing the file
function Description Syntax
fopen()
Used to open an existing file or to
create a new file
FILE *fopen(“file_name”,
“mode”);
fprintf() Used to write data in existing file
fprintf(FILE *stream, const char
*format [, argument, ...])
fscanf() Used to read data from the file
fscanf(FILE *stream, const char
*format [, argument, ...])
fputc() Used to write characters in a file fputc(int c, FILE *stream)
fgetc() Used to read characters from a file fgetc(FILE *stream)
fclose() Used to close existing file fclose( FILE *fp )
fseek()
puts the file pointer to the specified
place
fseek(FILE *stream, long int
offset, int whence)
File Pointer in C
When you want to perform file operations in C like
reading from or writing to a file, you need a way to
reference that file within your program. This reference is
provided by the file pointer. The file pointer stores the
address of a FILE structure, which contains details about
the file, like its name, its current position, its size, etc.
Syntax of File Pointer:
FILE *pointer_name;
FILE is a predefined data type in C, defined in the stdio.h header file. It represents a
file type.
* indicates that pointer_name is a pointer.
pointer_name can be any valid variable name, and it will become the name of the
file pointer.
For example, if you want to declare a file pointer named fptr, you'd write:
FILE *fptr;
Later on, when you open a file, you will use functions like fopen() which will return a
file pointer, and you can assign this to your declared file pointer:
fptr = fopen("filename.txt", "r");
This fptr can then be used with other functions to perform various operations on the
file "filename.txt".
Mode Description
r
Opens a file for reading. The file must
exist.
w
Opens or creates a file for writing. If file
exists, its contents are overwritten.
a
Opens a file for appending. If file
doesn't exist, it's created.
r+
Opens a file for both reading and
writing. The file must exist.
w+
Opens a file for both reading and
writing. It creates the file if it doesn't
exist.
a+
Opens a file for both reading and
appending. It creates the file if it
doesn't exist.
rb Opens a binary file in reading mode.
wb
Opens or creates a binary file in writing
mode.
ab Opens a binary file in append mode.
rb+
Opens a binary file for both reading and
writing.
wb+
Opens or creates a binary file for both
reading and writing.
ab+
Opens a binary file for both reading and
appending.
Example of Opening a File:
#include<stdio.h>
int main() {
FILE *fptr;
fptr = fopen("example.txt", "r"); // Open the file in read
mode
if (fptr == NULL) {
printf("Error in opening the file.n");
return 1; // Exit the program if file can't be opened
}
printf("File opened successfully.n");
fclose(fptr); // It's a good practice to close the file after
operations are done
return 0;
}

More Related Content

Similar to File handling in C hhsjsjshsjjsjsjs.pptx

Similar to File handling in C hhsjsjshsjjsjsjs.pptx (20)

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
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'
 
File handling4.pdf
File handling4.pdfFile handling4.pdf
File handling4.pdf
 
File Organization
File OrganizationFile Organization
File Organization
 
Topic - File operation.pptx
Topic - File operation.pptxTopic - File operation.pptx
Topic - File operation.pptx
 
File handling C program
File handling C programFile handling C program
File handling C program
 
File handling3.pdf
File handling3.pdfFile handling3.pdf
File handling3.pdf
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
 
7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
 
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
 
File handling and Dictionaries in python
File handling and Dictionaries in pythonFile handling and Dictionaries in python
File handling and Dictionaries in python
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
 
Python Files I_O17.pdf
Python Files I_O17.pdfPython Files I_O17.pdf
Python Files I_O17.pdf
 
File handling-c
File handling-cFile handling-c
File handling-c
 
Unit 8
Unit 8Unit 8
Unit 8
 
file_c.pdf
file_c.pdffile_c.pdf
file_c.pdf
 
File handing in C
File handing in CFile handing in C
File handing in C
 
data file handling
data file handlingdata file handling
data file handling
 
File Handling in C
File Handling in CFile Handling in C
File Handling in C
 
Python-files
Python-filesPython-files
Python-files
 

Recently uploaded

CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 

Recently uploaded (20)

CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 

File handling in C hhsjsjshsjjsjsjs.pptx

  • 1. File handling in C File handling in C stores data of our program in our local store, which can be used at any time because as the execution of a program completes, our data is lost. Therefore, we need to save our data in any file form - text or binary files. We can perform tasks like opening, reading the existing file, writing or adding new content, creating a new file, closing the file, or even saving the file to a certain location using C.
  • 2. A few features of using files •Reusability: Data stored in files can be used repeatedly. •Portability: Files enable data transfer between different systems. •Efficient: Quick access to stored data. •Storage Capacity: Large amounts of data can be stored beyond the constraints of RAM.
  • 3. Types of Files in C We will be working with two types of files: - 1.Text file 2.Binary file Let’s understand them in brief - Text file - The user can create these files easily while handling files in C. It stores information in the form of ASCII characters internally, and when the file is opened, the content is readable by humans. It can be created by any text editor with a .txt or .rtf (rich text)extension. Since text files are simple, they can be edited by any text editor like Microsoft Word, Notepad, Apple Text Edit, etc. Binary file - It stores information in the form of 0’s or 1’s, and it is saved with the .bin extension, taking less space. Since it is stored in a binary number system format, it is not readable by humans. Therefore it is more secure than a text file.
  • 4. C File Operations There are different kinds of file operations in C. 1.Creating a new file 2.Opening an existing file 3.Writing data to a file 4.Reading data from an existing file. 5.Moving data to a specific location on the file 6.Closing the file
  • 5. function Description Syntax fopen() Used to open an existing file or to create a new file FILE *fopen(“file_name”, “mode”); fprintf() Used to write data in existing file fprintf(FILE *stream, const char *format [, argument, ...]) fscanf() Used to read data from the file fscanf(FILE *stream, const char *format [, argument, ...]) fputc() Used to write characters in a file fputc(int c, FILE *stream) fgetc() Used to read characters from a file fgetc(FILE *stream) fclose() Used to close existing file fclose( FILE *fp ) fseek() puts the file pointer to the specified place fseek(FILE *stream, long int offset, int whence)
  • 6. File Pointer in C When you want to perform file operations in C like reading from or writing to a file, you need a way to reference that file within your program. This reference is provided by the file pointer. The file pointer stores the address of a FILE structure, which contains details about the file, like its name, its current position, its size, etc.
  • 7. Syntax of File Pointer: FILE *pointer_name; FILE is a predefined data type in C, defined in the stdio.h header file. It represents a file type. * indicates that pointer_name is a pointer. pointer_name can be any valid variable name, and it will become the name of the file pointer. For example, if you want to declare a file pointer named fptr, you'd write: FILE *fptr;
  • 8. Later on, when you open a file, you will use functions like fopen() which will return a file pointer, and you can assign this to your declared file pointer: fptr = fopen("filename.txt", "r"); This fptr can then be used with other functions to perform various operations on the file "filename.txt".
  • 9. Mode Description r Opens a file for reading. The file must exist. w Opens or creates a file for writing. If file exists, its contents are overwritten. a Opens a file for appending. If file doesn't exist, it's created. r+ Opens a file for both reading and writing. The file must exist. w+ Opens a file for both reading and writing. It creates the file if it doesn't exist. a+ Opens a file for both reading and appending. It creates the file if it doesn't exist. rb Opens a binary file in reading mode. wb Opens or creates a binary file in writing mode. ab Opens a binary file in append mode. rb+ Opens a binary file for both reading and writing. wb+ Opens or creates a binary file for both reading and writing. ab+ Opens a binary file for both reading and appending.
  • 10. Example of Opening a File: #include<stdio.h> int main() { FILE *fptr; fptr = fopen("example.txt", "r"); // Open the file in read mode if (fptr == NULL) { printf("Error in opening the file.n"); return 1; // Exit the program if file can't be opened } printf("File opened successfully.n"); fclose(fptr); // It's a good practice to close the file after operations are done return 0; }