SlideShare a Scribd company logo
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

PPTX
File handling in C
PPTX
File in C language
PPTX
Programming in C Session 4
PPTX
File Commands - R.D.Sivakumar
PPT
File Management
PPTX
File Management in C
PDF
FILES IN C
File handling in C
File in C language
Programming in C Session 4
File Commands - R.D.Sivakumar
File Management
File Management in C
FILES IN C

What's hot (20)

PPT
File in C Programming
PDF
Files in c
PPTX
File handling in C
PPTX
File handling in c
DOCX
Understanding c file handling functions with examples
PPTX
C Programming Unit-5
PPT
File in c
PPTX
File handling in c
PPTX
File Handling and Command Line Arguments in C
PPSX
1file handling
PPT
File handling-c programming language
PPT
File handling in c
PDF
Python-files
PPT
File handling in c
PPT
7.0 files and c input
PPT
File handling in 'C'
PPTX
File Handling in C
PDF
Module 03 File Handling in C
PPT
File handling in c
File in C Programming
Files in c
File handling in C
File handling in c
Understanding c file handling functions with examples
C Programming Unit-5
File in c
File handling in c
File Handling and Command Line Arguments in C
1file handling
File handling-c programming language
File handling in c
Python-files
File handling in c
7.0 files and c input
File handling in 'C'
File Handling in C
Module 03 File Handling in C
File handling in c
Ad

Viewers also liked (12)

Ad

Similar to Introduction to c part 4 (20)

PPT
How to do file-handling - in C language
PPTX
File management
PPSX
File mangement
PPTX
filehandling.pptx
PPTX
File handling With Solve Programs
PDF
VIT351 Software Development VI Unit5
PDF
file_c.pdf
PPTX
Working with files in c++. file handling
PPTX
File management
PDF
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
PPTX
File handing in C
PPTX
File management
PDF
637225560972186380.pdf
DOCX
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
PDF
Filehadnling
PPT
Unit5
PPTX
MODULE 8-File and preprocessor.pptx for c program learners easy learning
PDF
File Handling in C Programming
PPTX
File Management in C
PPTX
files c programming handling in computer programming
How to do file-handling - in C language
File management
File mangement
filehandling.pptx
File handling With Solve Programs
VIT351 Software Development VI Unit5
file_c.pdf
Working with files in c++. file handling
File management
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
File handing in C
File management
637225560972186380.pdf
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
Filehadnling
Unit5
MODULE 8-File and preprocessor.pptx for c program learners easy learning
File Handling in C Programming
File Management in C
files c programming handling in computer programming

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

PPTX
Agile methodology and scrum development
PDF
Acquiring new skills what you should know
PDF
Baabtra.com programming at school
PDF
99LMS for Enterprises - LMS that you will love
PPTX
Chapter 6 database normalisation
PPTX
Chapter 5 transactions and dcl statements
PPTX
Chapter 4 functions, views, indexing
PPTX
PPTX
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
PPTX
Chapter 1 introduction to sql server
PPTX
Chapter 1 introduction to sql server
Agile methodology and scrum development
Acquiring new skills what you should know
Baabtra.com programming at school
99LMS for Enterprises - LMS that you will love
Chapter 6 database normalisation
Chapter 5 transactions and dcl statements
Chapter 4 functions, views, indexing
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 1 introduction to sql server
Chapter 1 introduction to sql server

Recently uploaded (20)

PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Machine learning based COVID-19 study performance prediction
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Modernizing your data center with Dell and AMD
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
cuic standard and advanced reporting.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
KodekX | Application Modernization Development
PPTX
Cloud computing and distributed systems.
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Machine learning based COVID-19 study performance prediction
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Review of recent advances in non-invasive hemoglobin estimation
Modernizing your data center with Dell and AMD
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Electronic commerce courselecture one. Pdf
cuic standard and advanced reporting.pdf
MYSQL Presentation for SQL database connectivity
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
The AUB Centre for AI in Media Proposal.docx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
KodekX | Application Modernization Development
Cloud computing and distributed systems.
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
“AI and Expert System Decision Support & Business Intelligence Systems”
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Per capita expenditure prediction using model stacking based on satellite ima...

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