SlideShare a Scribd company logo
1 of 28
Recall
• What are the difference between stack
and heap?
• How to allocate memory in stack? And
heap?
• What are the difference between malloc()
and calloc()
Introduction to C
File handling in C
Week 3- day 2
Relevance of File handling in C
Relevance of File handling in C
Instruction Address
11011110 00110000
00010010 00110001
10000000 00001010
01001000 10000001
00001100 10000100
11000001 00101011
01011011 01011001
11101011 11111000
Mother Board
Ram
Loads instruction for execution
CPU
Ram Stores data
whenever the power is
on
Instruction Address
Ram
Loads instruction for execution
CPU
Mother Board
Relevance of File handling in C
Ram looses all data
whenever the power is OFF
as it is volatile. So we need a
mechanism to store data
permanently
Instruction Address
Ram
Loads instruction for execution
CPU
Mother Board
Relevance of File handling in C
Keeps all data in a File
and stores it on
secondary disks such
as hard disk, CD etc
You Should know !
Whenever we open a
file it will be loaded
into RAM memory .
Instruction Address
11011110 00110000
00010010 00110001
10000000 00001010
01001000 10000001
00001100 10000100
11000001 00101011
01011011 01011001
11101011 11111000
Mother Board
Ram
Loads instruction for execution
CPU
File Handling
• Files are created for permanent
storage of data
• When a computer reads a file, it
copies the file from the storage device
to memory; when it writes to a file, it
transfers data from memory to the
storage device.
Sample Program
File Handling
FILE *p;
*p=fopen(“data.txt”,”w”)
fprintf(p,“hello World”);
fclose(); First you need a File
pointer to the memory
location where the file is
loaded
File Handling
FILE *p;
*p=fopen(“data.txt”,”w”)
fprintf(p,“hello World”);
fclose();
Fopen() opens the file in
different modes and
returns the address
File Handling
FILE *p;
*p=fopen(“data.txt”,”w”)
fprintf(p,“hello World”);
fclose(); fprintf() is used to write in
to a file same like we use
printf to write on the
screen
File Handling
FILE *p;
*p=fopen(“data.txt”,”r”)
fprintf(p,“hello World”);
fclose(p);
File must be closed at the
end of program using
inbuilt function fclose()
File Modes
• r : reads from a file
• w : overwrite to a file
• a : append on a file
• r+ : reads and writes. File
pointer at the beginning
• w+ : reads and overwrites.
• a+ : reads and appends .File
pointer at the beginning
Write to file
• fprintf(p,” hello %s”,name);
• // same like printf() writes characters into a
file
• fputs(“message”,p);
• // same purpose of fprintf()
• fputc(„h‟,p);
• // writes a single character into file
Read from File
• fscanf(p,”%s”,message);
• // same like scanf() reads characters from
file and assign to a variable
• fgets(message,100,p);
• // same purpose of fscanf() but can mention
the number of characters to be read
• fgetc(p);
• // returns the current character and
advance the file pointer once; returns EOF
when file ending has reached
#include <stdio.h>
void main ()
{
FILE *fp;
int c,n=0;
fp = fopen("file.txt","r");
if(fp == NULL)
{
printf("Error in opening file");
Exit(0);
}
do
{
c = fgetc(fp);// fgetc() always return integer .if it is character it returns corresponding integer
printf("%c", c);
}
while(c != EOF);
fclose(fp);
}
Questions?
“A good question deserve a good
grade…”
Self Check !!
Self Check
• The first and second arguments of fopen
are
a) A character string containing the name of the file &
the second argument is the mode.
b) A character string containing the name of the user &
the second argument is the mode.
c) A character string containing file poniter & the
second argument is the mode.
d) None of the mentioned of the mentioned
Self Check
• The first and second arguments of fopen
are
a) A character string containing the name of the file &
the second argument is the mode.
b) A character string containing the name of the user &
the second argument is the mode.
c) A character string containing file poniter & the
second argument is the mode.
d) None of the mentioned of the mentioned
Self Check
• If there is any error while opening a file, fopen
will return
a) Nothing
b) EOF
c) NULL
d) Depends on compiler
Self Check
• If there is any error while opening a file, fopen
will return
a) Nothing
b) EOF
c) NULL
d) Depends on compiler
Self Check
• FILE is of type ______ ?
a) int type
b) char * type
c) struct type
d) None of the mentioned
Self Check
• FILE is of type ______ ?
a) int type
b) char * type
c) struct type
d) None of the mentioned
Self Check
• Which of the following mode argument is used
to truncate?
a) a
b) f
c) w
d) t
Self Check
• Which of the following mode argument is used
to truncate?
a) a
b) f
c) w
d) t
End of Day 2

More Related Content

What's hot (20)

File in C Programming
File in C ProgrammingFile in C Programming
File in C Programming
 
Files in c
Files in cFiles in c
Files in c
 
File handling in C
File handling in CFile handling in C
File handling in C
 
File handling in c
File handling in cFile handling in c
File handling in c
 
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 in c
File in cFile in c
File in c
 
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 Handling and Command Line Arguments in C
File Handling and Command Line Arguments in CFile Handling and Command Line Arguments in C
File Handling and Command Line Arguments in C
 
1file handling
1file handling1file handling
1file handling
 
File handling-c programming language
File handling-c programming languageFile handling-c programming language
File handling-c programming language
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Python-files
Python-filesPython-files
Python-files
 
File handling in c
File  handling in cFile  handling in c
File handling in c
 
7.0 files and c input
7.0 files and c input7.0 files and c input
7.0 files and c input
 
File 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 in C
File Handling in C
 
Module 03 File Handling in C
Module 03 File Handling in CModule 03 File Handling in C
Module 03 File Handling in C
 
File handling in c
File handling in c File handling in c
File handling in c
 

Viewers also liked (12)

Introduction to mysql part 5
Introduction to mysql part 5Introduction to mysql part 5
Introduction to mysql part 5
 
How to get a job in it?
How to get a job in it?How to get a job in it?
How to get a job in it?
 
Code optimization
Code optimization Code optimization
Code optimization
 
Different dimensions of android development baabtra.com
Different dimensions of android development baabtra.comDifferent dimensions of android development baabtra.com
Different dimensions of android development baabtra.com
 
scope of variables
scope of variablesscope of variables
scope of variables
 
Functions with heap and stack
Functions with heap and stackFunctions with heap and stack
Functions with heap and stack
 
Mvc
MvcMvc
Mvc
 
C# loops
C# loopsC# loops
C# loops
 
Error handling in ASP.NET
Error handling in ASP.NETError handling in ASP.NET
Error handling in ASP.NET
 
Transaction
TransactionTransaction
Transaction
 
Dom
DomDom
Dom
 
Intoduction to php strings
Intoduction to php  stringsIntoduction to php  strings
Intoduction to php strings
 

Similar to Introduction to c part 4

Similar to Introduction to c part 4 (20)

File management
File managementFile management
File management
 
File mangement
File mangementFile mangement
File mangement
 
filehandling.pptx
filehandling.pptxfilehandling.pptx
filehandling.pptx
 
File handling With Solve Programs
File handling With Solve ProgramsFile handling With Solve Programs
File handling With Solve Programs
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
 
file_c.pdf
file_c.pdffile_c.pdf
file_c.pdf
 
File management
File managementFile management
File management
 
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
 
File handing in C
File handing in CFile handing in C
File handing in C
 
File management
File managementFile management
File management
 
637225560972186380.pdf
637225560972186380.pdf637225560972186380.pdf
637225560972186380.pdf
 
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDYC UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
 
Filehadnling
FilehadnlingFilehadnling
Filehadnling
 
Unit5
Unit5Unit5
Unit5
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C Programming
 
File Management in C
File Management in CFile Management in C
File Management in C
 
Data file handling
Data file handlingData file handling
Data file handling
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
 
File Handling in C
File Handling in CFile Handling in C
File Handling in C
 
File Handling
File HandlingFile Handling
File Handling
 

More from baabtra.com - No. 1 supplier of quality freshers

More from baabtra.com - No. 1 supplier of quality freshers (20)

Agile methodology and scrum development
Agile methodology and scrum developmentAgile methodology and scrum development
Agile methodology and scrum development
 
Best coding practices
Best coding practicesBest coding practices
Best coding practices
 
Core java - baabtra
Core java - baabtraCore java - baabtra
Core java - baabtra
 
Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Microsoft holo lens
 
Blue brain
Blue brainBlue brain
Blue brain
 
5g
5g5g
5g
 
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra
Aptitude skills baabtra
 
Gd baabtra
Gd baabtraGd baabtra
Gd baabtra
 

Recently uploaded

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 

Recently uploaded (20)

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 

Introduction to c part 4

  • 1. Recall • What are the difference between stack and heap? • How to allocate memory in stack? And heap? • What are the difference between malloc() and calloc()
  • 2. Introduction to C File handling in C Week 3- day 2
  • 3. Relevance of File handling in C
  • 4. Relevance of File handling in C Instruction Address 11011110 00110000 00010010 00110001 10000000 00001010 01001000 10000001 00001100 10000100 11000001 00101011 01011011 01011001 11101011 11111000 Mother Board Ram Loads instruction for execution CPU Ram Stores data whenever the power is on
  • 5. Instruction Address Ram Loads instruction for execution CPU Mother Board Relevance of File handling in C Ram looses all data whenever the power is OFF as it is volatile. So we need a mechanism to store data permanently
  • 6. Instruction Address Ram Loads instruction for execution CPU Mother Board Relevance of File handling in C Keeps all data in a File and stores it on secondary disks such as hard disk, CD etc
  • 7. You Should know ! Whenever we open a file it will be loaded into RAM memory . Instruction Address 11011110 00110000 00010010 00110001 10000000 00001010 01001000 10000001 00001100 10000100 11000001 00101011 01011011 01011001 11101011 11111000 Mother Board Ram Loads instruction for execution CPU
  • 8. File Handling • Files are created for permanent storage of data • When a computer reads a file, it copies the file from the storage device to memory; when it writes to a file, it transfers data from memory to the storage device.
  • 10. File Handling FILE *p; *p=fopen(“data.txt”,”w”) fprintf(p,“hello World”); fclose(); First you need a File pointer to the memory location where the file is loaded
  • 11. File Handling FILE *p; *p=fopen(“data.txt”,”w”) fprintf(p,“hello World”); fclose(); Fopen() opens the file in different modes and returns the address
  • 12. File Handling FILE *p; *p=fopen(“data.txt”,”w”) fprintf(p,“hello World”); fclose(); fprintf() is used to write in to a file same like we use printf to write on the screen
  • 13. File Handling FILE *p; *p=fopen(“data.txt”,”r”) fprintf(p,“hello World”); fclose(p); File must be closed at the end of program using inbuilt function fclose()
  • 14. File Modes • r : reads from a file • w : overwrite to a file • a : append on a file • r+ : reads and writes. File pointer at the beginning • w+ : reads and overwrites. • a+ : reads and appends .File pointer at the beginning
  • 15. Write to file • fprintf(p,” hello %s”,name); • // same like printf() writes characters into a file • fputs(“message”,p); • // same purpose of fprintf() • fputc(„h‟,p); • // writes a single character into file
  • 16. Read from File • fscanf(p,”%s”,message); • // same like scanf() reads characters from file and assign to a variable • fgets(message,100,p); • // same purpose of fscanf() but can mention the number of characters to be read • fgetc(p); • // returns the current character and advance the file pointer once; returns EOF when file ending has reached
  • 17. #include <stdio.h> void main () { FILE *fp; int c,n=0; fp = fopen("file.txt","r"); if(fp == NULL) { printf("Error in opening file"); Exit(0); } do { c = fgetc(fp);// fgetc() always return integer .if it is character it returns corresponding integer printf("%c", c); } while(c != EOF); fclose(fp); }
  • 18. Questions? “A good question deserve a good grade…”
  • 20. Self Check • The first and second arguments of fopen are a) A character string containing the name of the file & the second argument is the mode. b) A character string containing the name of the user & the second argument is the mode. c) A character string containing file poniter & the second argument is the mode. d) None of the mentioned of the mentioned
  • 21. Self Check • The first and second arguments of fopen are a) A character string containing the name of the file & the second argument is the mode. b) A character string containing the name of the user & the second argument is the mode. c) A character string containing file poniter & the second argument is the mode. d) None of the mentioned of the mentioned
  • 22. Self Check • If there is any error while opening a file, fopen will return a) Nothing b) EOF c) NULL d) Depends on compiler
  • 23. Self Check • If there is any error while opening a file, fopen will return a) Nothing b) EOF c) NULL d) Depends on compiler
  • 24. Self Check • FILE is of type ______ ? a) int type b) char * type c) struct type d) None of the mentioned
  • 25. Self Check • FILE is of type ______ ? a) int type b) char * type c) struct type d) None of the mentioned
  • 26. Self Check • Which of the following mode argument is used to truncate? a) a b) f c) w d) t
  • 27. Self Check • Which of the following mode argument is used to truncate? a) a b) f c) w d) t