SlideShare a Scribd company logo
File Handling
Md. Imran Hossain Showrov (showrovsworld@gmail.com)
20
1
Outline
 What is a File?
 Steps in Processing a File
 The Basic File Operations
 File Open Modes
 File Open
 Closing a File
 Reading and Writing Files
What is a File?
 A file is a collection of related data that a computers treats
as a single unit.
 Computers store files to secondary storage so that the
contents of files remain intact when a computer shuts
down.
 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.
 C uses a structure called FILE (defined in stdio.h) to store
the attributes of a file.
Steps in Processing a File
1. Create the stream via a pointer variable using the
FILE structure:
FILE *p;
2. Open the file, associating the stream name with the
file name.
3. Read or write the data.
4. Close the file.
The Basic File Operations
 fopen - open a file- specify how its opened
(read/write) and type (binary/text)
 fclose - close an opened file
 fread - read from a file
 fwrite - write to a file
 fseek/fsetpos - move a file pointer to somewhere in a
file.
 ftell/fgetpos - tell you where the file pointer is
located.
File Open Modes
File Open Modes: In Addition
 r+ - open for reading and writing, start at beginning
 w+ - open for reading and writing (overwrite file)
 a+ - open for reading and writing (append if file
exists)
File Open
 The file open function (fopen) serves two purposes:
 It makes the connection between the physical file
and the stream.
 It creates “a program file structure to store the
information” C needs to process the file.
 Syntax:
filepointer=fopen(“filename”,“mode”);
More On fopen
 The file mode tells C how the program will use the
file.
 The filename indicates the system name and location
for the file.
 We assign the return value of fopen to our pointer
variable:
spData = fopen(“MYFILE.TXT”,“w”);
spData = fopen(“A:MYFILE.TXT”,“w”);
More On fopen
Closing a File
 When we finish with a mode, we need to close the
file before ending the program or beginning another
mode with that same file.
 To close a file, we use fclose and the pointer
variable:
fclose(spData);
fprintf()
Syntax:
fprintf (fp,"string",variables);
Example:
– int i = 12;
– float x = 2.356;
– char ch = 's';
– FILE *fp;
– fp=fopen(“out.txt”,”w”);
– fprintf (fp, "%d %f %c", i, x, ch);
fscanf()
Syntax:
fscanf (fp,"string",identifiers);
Example:
– FILE *fp;
– Fp=fopen(“input.txt”,”r”);
– int i;
– fscanf (fp,“%d",i);
getc()
Syntax:
identifier = getc (file pointer);
Example:
FILE *fp;
fp=fopen(“input.txt”,”r”);
char ch;
ch = getc (fp);
putc()
 Write a single character to the output file, pointed to
by fp.
Example:
FILE *fp;
char ch;
putc (ch,fp);
End of File
 There are a number of ways to test for the end-of-file
condition. Another way is to use the value returned by the
fscanf function:
FILE *fptr1;
int istatus ;
istatus = fscanf (fptr1, "%d", &var) ;
if ( istatus == feof(fptr1) )
{
printf ("End-of-file encountered.n”) ;
}
Reading and Writing Files
#include <stdio.h>
int main ( )
{
FILE *outfile, *infile ;
int b = 5, f ;
float a = 13.72, c = 6.68, e, g ;
outfile = fopen ("testdata", "w") ;
fprintf (outfile,“ %f %d %f ", a, b, c) ;
fclose (outfile) ;
infile = fopen ("testdata", "r") ;
fscanf (infile,"%f %d %f", &e, &f, &g) ;
printf (“ %f %d %f n ", a, b, c) ;
printf (“ %f %d %f n ", e, f, g) ;
}
Example
#include <stdio.h>
#include<conio.h>
void main()
{
char ch;
FILE *fp;
fp=fopen("out.txt","r");
while(!feof(fp))
{
ch=getc(fp);
printf("n%c",ch);
}
getch();
}
fread ()
Declaration:
size_t fread(void *ptr, size_t size, size_t n, FILE *stream);
Remarks:
fread reads a specified number of equal-sized data items from
an input stream into a block.
ptr = Points to a block into which data is read
size = Length of each item read, in bytes
n = Number of items read
stream = file pointer
Example
#include <stdio.h>  
int main()
{
FILE *f;  
char buffer[11];
if (f = fopen("fred.txt",“r”))
{
fread(buffer, 1, 10, f);
buffer[10] = 0;
fclose(f);
printf("first 10 characters of the file:n%sn", buffer);
}  
return 0;
}
fwrite()
Declaration:
size_t fwrite(const void *ptr, size_t size, size_t n, FILE*stream);
Remarks:
fwrite appends a specified number of equal-sized data items to an
output file.
ptr = Pointer to any object; the data written begins at ptr
size = Length of each item of data
n =Number of data items to be appended
stream = file pointer
Example
#include <stdio.h>
int main()
{
char a[10]={'1','2','3','4','5','6','7','8','9','a'};
FILE *fs;
fs=fopen("Project.txt","w");
fwrite(a,1,10,fs);
fclose(fs);
return 0;
}
Lecture 20 - File Handling

More Related Content

PPSX
C programming file handling
PPTX
File handling in c
PPTX
File Management in C
PPT
File Management
PPT
File in c
PPTX
File handling in C
C programming file handling
File handling in c
File Management in C
File Management
File in c
File handling in C

What's hot (20)

PPT
File in C Programming
PPT
File handling in 'C'
PPT
File handling in c
PDF
Module 03 File Handling in C
PPTX
File handling in C by Faixan
PPSX
1file handling
PPTX
File handling in C
PPT
file
PPTX
file management in c language
PPT
PPTX
File Management in C
PPT
File handling in c
PPTX
File in C programming
PPT
File handling
PDF
File_Management_in_C
PDF
Files in C
PPT
File management and handling by prabhakar
PPT
File handling-c programming language
PPT
File handling in c
PPT
PHP - Introduction to File Handling with PHP
File in C Programming
File handling in 'C'
File handling in c
Module 03 File Handling in C
File handling in C by Faixan
1file handling
File handling in C
file
file management in c language
File Management in C
File handling in c
File in C programming
File handling
File_Management_in_C
Files in C
File management and handling by prabhakar
File handling-c programming language
File handling in c
PHP - Introduction to File Handling with PHP
Ad

Similar to Lecture 20 - File Handling (20)

PPTX
File in C language
DOCX
PPTX
File management
PPTX
file handling in c programming with file functions
PPTX
want to learn files,then just use this ppt to learn
PPTX
PPS PPT 2.pptx
PPTX
Unit-VI.pptx
PDF
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
PPT
file_handling_in_c.ppt
PPT
Unit5
PDF
FILES IN C
PPT
Files_in_C.ppt
PPTX
File Handling ppt.pptx shjd dbkd z bdjdb d
PPTX
INput output stream in ccP Full Detail.pptx
PPTX
File handling in c
PPT
file_handling_in_c.ppt
PPT
File handling-dutt
PPTX
Data Structure Using C - FILES
DOCX
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
PDF
File handling C program
File in C language
File management
file handling in c programming with file functions
want to learn files,then just use this ppt to learn
PPS PPT 2.pptx
Unit-VI.pptx
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
file_handling_in_c.ppt
Unit5
FILES IN C
Files_in_C.ppt
File Handling ppt.pptx shjd dbkd z bdjdb d
INput output stream in ccP Full Detail.pptx
File handling in c
file_handling_in_c.ppt
File handling-dutt
Data Structure Using C - FILES
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
File handling C program
Ad

More from Md. Imran Hossain Showrov (20)

PPT
Lecture 22 - Error Handling
PPT
Lecture 21 - Preprocessor and Header File
PPT
Lecture 19 - Struct and Union
PPT
Lecture 18 - Pointers
PPT
Lecture 16 - Multi dimensional Array
PPT
Lecture 17 - Strings
PPT
Lecture 15 - Array
PPT
Lecture 14 - Scope Rules
PPT
Lecture 13 - Storage Classes
PPT
Lecture 12 - Recursion
PPT
Lecture 11 - Functions
PPT
Lecture 10 - Control Structures 2
PPT
Lecture 8- Data Input and Output
PPT
Lecture 9- Control Structures 1
PPT
Lecture 7- Operators and Expressions
PPT
Lecture 6- Intorduction to C Programming
PPT
Lecture 5 - Structured Programming Language
PPT
Lecture 4- Computer Software and Languages
PPT
Lecture 3 - Processors, Memory and I/O devices
PPT
Lecture 2 - Introductory Concepts
Lecture 22 - Error Handling
Lecture 21 - Preprocessor and Header File
Lecture 19 - Struct and Union
Lecture 18 - Pointers
Lecture 16 - Multi dimensional Array
Lecture 17 - Strings
Lecture 15 - Array
Lecture 14 - Scope Rules
Lecture 13 - Storage Classes
Lecture 12 - Recursion
Lecture 11 - Functions
Lecture 10 - Control Structures 2
Lecture 8- Data Input and Output
Lecture 9- Control Structures 1
Lecture 7- Operators and Expressions
Lecture 6- Intorduction to C Programming
Lecture 5 - Structured Programming Language
Lecture 4- Computer Software and Languages
Lecture 3 - Processors, Memory and I/O devices
Lecture 2 - Introductory Concepts

Recently uploaded (20)

PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
RMMM.pdf make it easy to upload and study
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Classroom Observation Tools for Teachers
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Lesson notes of climatology university.
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Pharma ospi slides which help in ospi learning
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Cell Structure & Organelles in detailed.
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
202450812 BayCHI UCSC-SV 20250812 v17.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
RMMM.pdf make it easy to upload and study
GDM (1) (1).pptx small presentation for students
Classroom Observation Tools for Teachers
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Lesson notes of climatology university.
Pharmacology of Heart Failure /Pharmacotherapy of CHF
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Final Presentation General Medicine 03-08-2024.pptx
Computing-Curriculum for Schools in Ghana
Pharma ospi slides which help in ospi learning
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Cell Structure & Organelles in detailed.
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx

Lecture 20 - File Handling

  • 2. Outline  What is a File?  Steps in Processing a File  The Basic File Operations  File Open Modes  File Open  Closing a File  Reading and Writing Files
  • 3. What is a File?  A file is a collection of related data that a computers treats as a single unit.  Computers store files to secondary storage so that the contents of files remain intact when a computer shuts down.  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.  C uses a structure called FILE (defined in stdio.h) to store the attributes of a file.
  • 4. Steps in Processing a File 1. Create the stream via a pointer variable using the FILE structure: FILE *p; 2. Open the file, associating the stream name with the file name. 3. Read or write the data. 4. Close the file.
  • 5. The Basic File Operations  fopen - open a file- specify how its opened (read/write) and type (binary/text)  fclose - close an opened file  fread - read from a file  fwrite - write to a file  fseek/fsetpos - move a file pointer to somewhere in a file.  ftell/fgetpos - tell you where the file pointer is located.
  • 7. File Open Modes: In Addition  r+ - open for reading and writing, start at beginning  w+ - open for reading and writing (overwrite file)  a+ - open for reading and writing (append if file exists)
  • 8. File Open  The file open function (fopen) serves two purposes:  It makes the connection between the physical file and the stream.  It creates “a program file structure to store the information” C needs to process the file.  Syntax: filepointer=fopen(“filename”,“mode”);
  • 9. More On fopen  The file mode tells C how the program will use the file.  The filename indicates the system name and location for the file.  We assign the return value of fopen to our pointer variable: spData = fopen(“MYFILE.TXT”,“w”); spData = fopen(“A:MYFILE.TXT”,“w”);
  • 11. Closing a File  When we finish with a mode, we need to close the file before ending the program or beginning another mode with that same file.  To close a file, we use fclose and the pointer variable: fclose(spData);
  • 12. fprintf() Syntax: fprintf (fp,"string",variables); Example: – int i = 12; – float x = 2.356; – char ch = 's'; – FILE *fp; – fp=fopen(“out.txt”,”w”); – fprintf (fp, "%d %f %c", i, x, ch);
  • 13. fscanf() Syntax: fscanf (fp,"string",identifiers); Example: – FILE *fp; – Fp=fopen(“input.txt”,”r”); – int i; – fscanf (fp,“%d",i);
  • 14. getc() Syntax: identifier = getc (file pointer); Example: FILE *fp; fp=fopen(“input.txt”,”r”); char ch; ch = getc (fp);
  • 15. putc()  Write a single character to the output file, pointed to by fp. Example: FILE *fp; char ch; putc (ch,fp);
  • 16. End of File  There are a number of ways to test for the end-of-file condition. Another way is to use the value returned by the fscanf function: FILE *fptr1; int istatus ; istatus = fscanf (fptr1, "%d", &var) ; if ( istatus == feof(fptr1) ) { printf ("End-of-file encountered.n”) ; }
  • 17. Reading and Writing Files #include <stdio.h> int main ( ) { FILE *outfile, *infile ; int b = 5, f ; float a = 13.72, c = 6.68, e, g ; outfile = fopen ("testdata", "w") ; fprintf (outfile,“ %f %d %f ", a, b, c) ; fclose (outfile) ; infile = fopen ("testdata", "r") ; fscanf (infile,"%f %d %f", &e, &f, &g) ; printf (“ %f %d %f n ", a, b, c) ; printf (“ %f %d %f n ", e, f, g) ; }
  • 18. Example #include <stdio.h> #include<conio.h> void main() { char ch; FILE *fp; fp=fopen("out.txt","r"); while(!feof(fp)) { ch=getc(fp); printf("n%c",ch); } getch(); }
  • 19. fread () Declaration: size_t fread(void *ptr, size_t size, size_t n, FILE *stream); Remarks: fread reads a specified number of equal-sized data items from an input stream into a block. ptr = Points to a block into which data is read size = Length of each item read, in bytes n = Number of items read stream = file pointer
  • 20. Example #include <stdio.h>   int main() { FILE *f;   char buffer[11]; if (f = fopen("fred.txt",“r”)) { fread(buffer, 1, 10, f); buffer[10] = 0; fclose(f); printf("first 10 characters of the file:n%sn", buffer); }   return 0; }
  • 21. fwrite() Declaration: size_t fwrite(const void *ptr, size_t size, size_t n, FILE*stream); Remarks: fwrite appends a specified number of equal-sized data items to an output file. ptr = Pointer to any object; the data written begins at ptr size = Length of each item of data n =Number of data items to be appended stream = file pointer
  • 22. Example #include <stdio.h> int main() { char a[10]={'1','2','3','4','5','6','7','8','9','a'}; FILE *fs; fs=fopen("Project.txt","w"); fwrite(a,1,10,fs); fclose(fs); return 0; }