SlideShare a Scribd company logo
MSc. BIOTECHNOLOGY
PRESENTED TO: Ms. HIMANI VERMA
PRESENTED BY: TANYA(2084043)
MUGDHA(2084044)
Concept of file handling
CONTENT-
INTRODUCTION
WHAT IS A FILE?
TYPES OF FILES
NEED OF FILE HANDLING
STEPS FOR PROCESSING A FILE
FILE INPUT/OUTPUT FUNCTIONS
 DECLARATION OF A FILE
 OPENING OF A FILE
 READING DATA FROM A FILE
 WRITING DATA IN A FILE
 CLOSING THE FILE
PROGRAMS
WHAT IS A FILE?
 A FILE IS A COLLECTION OF DATA STORED IN ONE UNIT,
IDENTIFIED BY A FILENAME.
 IT CAN BE A DOCUMENT, PICTURE, AUDIO OR VIDEO STREAM,
DATA LIBRARY, APPLICATION, OR OTHER COLLECTION OF DATA.
 FILES CAN BE OPENED, SAVED, DELETED, AND MOVED TO
DIFFERENT FOLDERS.
 THEY CAN ALSO BE TRANSFERRED ACROSS NETWORK
CONNECTIONS OR DOWNLOADED FROM THE INTERNET.
 A FILES TYPE CAN BE DETERMINED BY VIEWING THE FILES
ICON OR BY READING THE FILE EXTENSION.
INTRODUCTION
 FILE HANDLING IS STORING OF DATA IN A FILE USING A
PROGRAM.
 WE CAN EXTRACT/ FETCH DATA FROM A FILE TO WORK WITH
IT IN THE PROGRAM.
 FILES ARE USED TO STORE DATA IN A STORAGE DEVICE
PERMANENTLY.
 FILE HANDLING PROVIDES A MECHANISMTO STORE THE
OUTPUT OF A PROGRAM IN A FILE AND TO PERFORM VARIOUS
OPERATIONS ON IT.
TYPES OF FILES
TEXT FILES - IT IS THE DEFAULT MODE OF A FILE.
EACH LINE IN A TEXT FILE IS TERMINATED WITH THE SPECIAL
CHARACTER KNOWN AS END OF LINE. THE EXTENSION OF TEXT
FILE IS .txt.
BINARY FILES – IT CONTAINS SAME FORMAT IN WHICH THE
INFORMATION IS HELD IN THE MEMORY. NO TRANSLATIONS ARE
REQUIRED IN THIS FILE.
CSV FILES – THIS IS THE COMMA SEPARATED VALUES FILE, WHICH
ALLOWS DATA TO BE SAVED IN TABULAR FORM. THESE ARE USE
TEXT FILES BINARY
FILES
CSV FILES
need of file handling
 A FILE IN ITSELF IS A BUNCH OF BYTES STORED ON
SOME STORAGE DEVICE.
 FILE HELPS US TO STORE DATA PERMANENTLY, WHICH
CAN BE RETRIEVED IN FUTURE.
 C - LANGUAGE PROVIDES A CONCEPT OF FILE THROUGH
WHICH DATA CAN BE STORED ON A DISK OR
SECONDARY STORAG DEVICE.
 THE STORED DATA CAN BE RETRIEVED WHENEVER
NEEDED.
 FILE HANDLING IS REQUIREDTO STORE THE OUTPUT OF
THE PREOGRAM WHICH CAN BE USED IN THE FUTURE.
 NO MATTER WHATEVER APPLICATION WE DEVELOP , WE
NEED TO STORE THAT DATA SOMEWHERE, AND HERE
DATA HANDLING COMES INTO PLAY.
STEPS FOR PROCESSING A
FILE
 DECLARE A FILE POINTER VARIABLE
 OPEN A FILE USING fopen() function
 PROCESS THE FILE USING SUITABLE
FUNCTION
 CLOSE THE FILE USING fclose()
function
FILE INPUT/OUTPUT FUNCTIONS
AVAILABLE IN THE STDIO LIBRARY
ARE:
DECLARATION OF A FILE
POINTER
• The type of file that is to be used must
be specified
• This is accomplished by using a
VARIABLE called FILE POINTER (fp)
• Pointer variable that points to a structure FILE
• The members of the FILE structure are used by the
program in various file access operations, but
programmers do not need to be concerned about
them.
• For each file that is to be OPENED, a
pointer type FILE must be declared.
FILE is a structure declared in stdio.h
 When the function fopen() is called, that function
creates an instance of the FILE structure and
returns a pointer to that structure .
 This pointer is used in all subsequent operations
on the file.
 The SYNTAX for declaring file pointers is as
follows; FILE *file_pointer_name,…;
FILE *fp;
OPENING A FILE: FOR
CREATION AND EDIT
 Opening a file is performed using the fopen() function
defined in the stdio.h header file.
 The syntax for opening a file in standard I/O is:
 Function fopen takes the name of a file as it's 1st
parameter
And mode as it's 2nd parameter.
 The string given as first parameter for filename, opens
the file named and assigns an identifier to the FILE type
pointer fp.
 This pointer, which contains all the information about
the file is subsequently used as a communication link
between the system and program.
 The string given as second parameter for mode,
FILE
*fopen("filename","mode");
A filename in a C program can also contain path information
The path specifies the drive and/or directory or folder name
where the file is located.
If a filename is specified without a path, it will be assumed
that the file is located wherever the operating system
currently designates as the default.
On PCs, the backslash character (  ) is used to separate
directory names in a path.
It is to be remembered that the backslash character has a
special meaning to C with respect to escape sequence when it
is in a string. To represent the backslash character itself, one
must precede it with another backslash.
Thus, in a C program, the filename would be represented as
follows.
“c:examdatalist.txt”;
Directory
name
Drive
name
File
name
Mode can be one of the following;
 If the purpose is " reading" and if it exists, then the file is opened
with current contents safe otherwise an error occurs.
 When the mode is " writing", a file with the specialised name is
created, if the file does not exist. The contents are deleted, if the
file already exists.
 When the purpose is " appending", the file is opened with the
current contents safe. A file with the specified name is created if
the file does not exist.
These statements are used to create a text file with
the name data.dat under current directory
It is opened in "w" mode as data are to be written into
the file data.dat
FILE *fp;
fp =
fopen("data.dat","w");
Following is an example where a file pointer “ fp” is declared, the file
name, which is declared to contain a maximum of 80 characters, is
obtained from the keyboard and then the file is opened in the “write”
mode.
char filename[80];
FILE *fp;
printf(“Enter the filename to be
opened”);
gets(fi lename);
fp = fopen(fi lename,“w”);
Writing data In a file
C provides four functions that can be used to
write text file into the disk. These are;
 fprintf()
 fputs()
 fputc()
 fwrite()
They are just the file versions of printf() puts()
putc() write()
The only difference is that fprintf() fputs()
fputc() fwrite() expects a pointer to the structure
FILE.
EXAMPLE; Write to a text file
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
FILE *fptr
fptr = fopen("C:program.txt","w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter num: ");
scanf("%d",&num);
fprintf(fptr,"%d",num);
fclose(fptr);
return 0;
}
Writing to a binary file
To write into a binary file, we need to use the
fwrite() function. The functions take 4
arguments:
1. Address of data to be written in the disk
2. Size of data to be written in the disk
3. Number of such type of data
4. Pointer to the file where you want to write
fwrite(addressData, sizeData, numbersData,
pointerToFile);
#include <stdio.h>
struct threeNum
{
int n1, n2, n3;
};
int main()
{
int n;
struct threeNum num;
FILE *fptr;
if ((fptr = fopen("C:program.bin","wb")) == NULL){
printf("Error! opening file");
exit(1);
}
for(n = 1; n < 5; ++n)
{
num.n1 = n;
num.n2 = 5*n;
num.n3 = 5*n + 1;
fwrite(&num, sizeof(struct threeNum), 1, fptr);
}
fclose(fptr);
return 0;
}
Reading data from a file
C provides 4 functions that can be used to read text
files from the disk
 fscanf()
 fgets()
 fgetc()
 fread()
These are just the file versions of scanf() gets()
getc() and read()
The only difference is that fscanf() fgets() fgetc()
and fread() expects a pointer to the structural FILE.
EXAMPLE: Read from a text file
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
FILE *fptr;
if ((fptr = fopen("C:program.txt","r")) == NULL){
printf("Error! opening file");
exit(1);
}
fscanf(fptr,"%d", &num);
printf("Value of n=%d", num);
fclose(fptr);
return 0;
}
READ FROM A BINARY FILE
Function fread() takes 4 arguments:
1. Address of data to be written in the disk
2. Size of data to be written in the disk
3. Number of such type of data
4. Pointer to the file where you want to read
fread(addressData, sizeData, numbersData,
pointerToFile)
Read from a binary file using fread()
#include <stdio.h>
#include <stdlib.h>
struct threeNum
{
int n1, n2, n3;
};
int main()
{
int n;
struct threeNum num;
FILE *fptr;
if ((fptr = fopen("C:program.bin","rb")) == NULL){
printf("Error! opening file");
exit(1);
}
for(n = 1; n < 5; ++n)
{
fread(&num, sizeof(struct threeNum), 1, fptr);
printf("n1: %dtn2: %dtn3: %d", num.n1, num.n2, num.n3);
}
fclose(fptr);
return 0:
}
Closing a file
The file (both text and binary) should be closed
after readingwriting.
Closing a file is performed using the fclose()
function.
Here, fptr is a file pointer associated with the
file to be closed.
fclose() returns 0 on success or –1 on error
fclose(fptr)
When a program terminates
(either by reaching the end of main() or by
executing the exit() function)
All streams are automatically flushed and
closed.
Actually, in a simple program, it is not
necessary to close the file because the system
closes all open files before returning to the
operating system.
programs
To open a file, write in it,
and close the file
To open a file, read from
it, and close the file
REFERENCES
https://www.programiz.com/c-programming/c-file-
input-output
https://www.geeksforgeeks.org/basics-file-handling-
c/
BOOKS:
 Programming in C by Pradip Dey and Manas
Ghosh
 Programming in ANSI by E Balagurusamy
 Computer Fundamentals by PK sinha and Priti
Sinha
This Photo by Unknown
author is licensed under CC
BY-NC.

More Related Content

What's hot

File Management
File ManagementFile Management
File Management
Ravinder Kamboj
 
File handling in C
File handling in CFile handling in C
File handling in C
Kamal Acharya
 
File in C Programming
File in C ProgrammingFile in C Programming
File in C Programming
Sonya Akter Rupa
 
File in C language
File in C languageFile in C language
File in C language
Manash Kumar Mondal
 
File handling in C by Faixan
File handling in C by FaixanFile handling in C by Faixan
File handling in C by Faixan
ٖFaiXy :)
 
File handling-dutt
File handling-duttFile handling-dutt
File handling-dutt
Anil Dutt
 
Unit 5 dwqb ans
Unit 5 dwqb ansUnit 5 dwqb ans
Unit 5 dwqb ans
Sowri Rajan
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10patcha535
 
File Handling in C
File Handling in C File Handling in C
File Handling in C
Subhanshu Maurya
 
file management in c language
file management in c languagefile management in c language
file management in c language
chintan makwana
 
File handling in c
File handling in c File handling in c
File handling in c
Vikash Dhal
 
Data Structure Using C - FILES
Data Structure Using C - FILESData Structure Using C - FILES
Data Structure Using C - FILES
Harish Kamat
 
Module 5 file cp
Module 5 file cpModule 5 file cp
Module 5 file cp
Amarjith C K
 
5 Structure & File.pptx
5 Structure & File.pptx5 Structure & File.pptx
5 Structure & File.pptx
aarockiaabinsAPIICSE
 
file handling1
file handling1file handling1
file handling1student
 
C 檔案輸入與輸出
C 檔案輸入與輸出C 檔案輸入與輸出
C 檔案輸入與輸出
PingLun Liao
 
Files and Directories in PHP
Files and Directories in PHPFiles and Directories in PHP
Files and Directories in PHP
Nicole Ryan
 
Python-files
Python-filesPython-files
Python-files
Krishna Nanda
 

What's hot (19)

File Management
File ManagementFile Management
File Management
 
File handling in C
File handling in CFile handling in C
File handling in C
 
File in C Programming
File in C ProgrammingFile in C Programming
File in C Programming
 
File in C language
File in C languageFile in C language
File in C language
 
File handling in C by Faixan
File handling in C by FaixanFile handling in C by Faixan
File handling in C by Faixan
 
File handling-dutt
File handling-duttFile handling-dutt
File handling-dutt
 
Unit 5 dwqb ans
Unit 5 dwqb ansUnit 5 dwqb ans
Unit 5 dwqb ans
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10
 
File Handling in C
File Handling in C File Handling in C
File Handling in C
 
file management in c language
file management in c languagefile management in c language
file management in c language
 
File handling in c
File handling in c File handling in c
File handling in c
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Data Structure Using C - FILES
Data Structure Using C - FILESData Structure Using C - FILES
Data Structure Using C - FILES
 
Module 5 file cp
Module 5 file cpModule 5 file cp
Module 5 file cp
 
5 Structure & File.pptx
5 Structure & File.pptx5 Structure & File.pptx
5 Structure & File.pptx
 
file handling1
file handling1file handling1
file handling1
 
C 檔案輸入與輸出
C 檔案輸入與輸出C 檔案輸入與輸出
C 檔案輸入與輸出
 
Files and Directories in PHP
Files and Directories in PHPFiles and Directories in PHP
Files and Directories in PHP
 
Python-files
Python-filesPython-files
Python-files
 

Similar to Concept of file handling in c

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
sudhakargeruganti
 
File handling-c
File handling-cFile handling-c
File Organization
File OrganizationFile Organization
File Organization
RAMPRAKASH REDDY ARAVA
 
PPS-II UNIT-5 PPT.pptx
PPS-II  UNIT-5 PPT.pptxPPS-II  UNIT-5 PPT.pptx
PPS-II UNIT-5 PPT.pptx
VenkataRangaRaoKommi1
 
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
sangeeta borde
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
DHARUNESHBOOPATHY
 
Files in C
Files in CFiles in C
Files in C
Prabu U
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
Sandeepbhuma1
 
File management
File managementFile management
File management
sumathiv9
 
Handout#01
Handout#01Handout#01
Handout#01
Sunita Milind Dol
 
file_c.pdf
file_c.pdffile_c.pdf
file_c.pdf
Osmania University
 
File handling
File handlingFile handling
File handling
Ans Ali
 
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
AssadLeo1
 
File_Handling in C.ppt
File_Handling in C.pptFile_Handling in C.ppt
File_Handling in C.ppt
lakshmanarao027MVGRC
 
File_Handling in C.ppt
File_Handling in C.pptFile_Handling in C.ppt
File_Handling in C.ppt
lakshmanarao027MVGRC
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
yuvrajkeshri
 

Similar to Concept of file handling in c (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
 
File handling-c
File handling-cFile handling-c
File handling-c
 
File Organization
File OrganizationFile Organization
File Organization
 
PPS-II UNIT-5 PPT.pptx
PPS-II  UNIT-5 PPT.pptxPPS-II  UNIT-5 PPT.pptx
PPS-II UNIT-5 PPT.pptx
 
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_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
Files in C
Files in CFiles in C
Files in C
 
Unit 8
Unit 8Unit 8
Unit 8
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
 
File management
File managementFile management
File management
 
Unit v
Unit vUnit v
Unit v
 
Unit5
Unit5Unit5
Unit5
 
Unit5 C
Unit5 C Unit5 C
Unit5 C
 
Handout#01
Handout#01Handout#01
Handout#01
 
file_c.pdf
file_c.pdffile_c.pdf
file_c.pdf
 
File handling
File handlingFile handling
File handling
 
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
 
File_Handling in C.ppt
File_Handling in C.pptFile_Handling in C.ppt
File_Handling in C.ppt
 
File_Handling in C.ppt
File_Handling in C.pptFile_Handling in C.ppt
File_Handling in C.ppt
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 

Recently uploaded

Richard's aventures in two entangled wonderlands
Richard's aventures in two entangled wonderlandsRichard's aventures in two entangled wonderlands
Richard's aventures in two entangled wonderlands
Richard Gill
 
3D Hybrid PIC simulation of the plasma expansion (ISSS-14)
3D Hybrid PIC simulation of the plasma expansion (ISSS-14)3D Hybrid PIC simulation of the plasma expansion (ISSS-14)
3D Hybrid PIC simulation of the plasma expansion (ISSS-14)
David Osipyan
 
bordetella pertussis.................................ppt
bordetella pertussis.................................pptbordetella pertussis.................................ppt
bordetella pertussis.................................ppt
kejapriya1
 
Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...
Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...
Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...
Travis Hills MN
 
Seminar of U.V. Spectroscopy by SAMIR PANDA
 Seminar of U.V. Spectroscopy by SAMIR PANDA Seminar of U.V. Spectroscopy by SAMIR PANDA
Seminar of U.V. Spectroscopy by SAMIR PANDA
SAMIR PANDA
 
nodule formation by alisha dewangan.pptx
nodule formation by alisha dewangan.pptxnodule formation by alisha dewangan.pptx
nodule formation by alisha dewangan.pptx
alishadewangan1
 
Introduction to Mean Field Theory(MFT).pptx
Introduction to Mean Field Theory(MFT).pptxIntroduction to Mean Field Theory(MFT).pptx
Introduction to Mean Field Theory(MFT).pptx
zeex60
 
DERIVATION OF MODIFIED BERNOULLI EQUATION WITH VISCOUS EFFECTS AND TERMINAL V...
DERIVATION OF MODIFIED BERNOULLI EQUATION WITH VISCOUS EFFECTS AND TERMINAL V...DERIVATION OF MODIFIED BERNOULLI EQUATION WITH VISCOUS EFFECTS AND TERMINAL V...
DERIVATION OF MODIFIED BERNOULLI EQUATION WITH VISCOUS EFFECTS AND TERMINAL V...
Wasswaderrick3
 
Nucleic Acid-its structural and functional complexity.
Nucleic Acid-its structural and functional complexity.Nucleic Acid-its structural and functional complexity.
Nucleic Acid-its structural and functional complexity.
Nistarini College, Purulia (W.B) India
 
platelets_clotting_biogenesis.clot retractionpptx
platelets_clotting_biogenesis.clot retractionpptxplatelets_clotting_biogenesis.clot retractionpptx
platelets_clotting_biogenesis.clot retractionpptx
muralinath2
 
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
yqqaatn0
 
The use of Nauplii and metanauplii artemia in aquaculture (brine shrimp).pptx
The use of Nauplii and metanauplii artemia in aquaculture (brine shrimp).pptxThe use of Nauplii and metanauplii artemia in aquaculture (brine shrimp).pptx
The use of Nauplii and metanauplii artemia in aquaculture (brine shrimp).pptx
MAGOTI ERNEST
 
20240520 Planning a Circuit Simulator in JavaScript.pptx
20240520 Planning a Circuit Simulator in JavaScript.pptx20240520 Planning a Circuit Simulator in JavaScript.pptx
20240520 Planning a Circuit Simulator in JavaScript.pptx
Sharon Liu
 
Deep Behavioral Phenotyping in Systems Neuroscience for Functional Atlasing a...
Deep Behavioral Phenotyping in Systems Neuroscience for Functional Atlasing a...Deep Behavioral Phenotyping in Systems Neuroscience for Functional Atlasing a...
Deep Behavioral Phenotyping in Systems Neuroscience for Functional Atlasing a...
Ana Luísa Pinho
 
What is greenhouse gasses and how many gasses are there to affect the Earth.
What is greenhouse gasses and how many gasses are there to affect the Earth.What is greenhouse gasses and how many gasses are there to affect the Earth.
What is greenhouse gasses and how many gasses are there to affect the Earth.
moosaasad1975
 
ANAMOLOUS SECONDARY GROWTH IN DICOT ROOTS.pptx
ANAMOLOUS SECONDARY GROWTH IN DICOT ROOTS.pptxANAMOLOUS SECONDARY GROWTH IN DICOT ROOTS.pptx
ANAMOLOUS SECONDARY GROWTH IN DICOT ROOTS.pptx
RASHMI M G
 
NuGOweek 2024 Ghent programme overview flyer
NuGOweek 2024 Ghent programme overview flyerNuGOweek 2024 Ghent programme overview flyer
NuGOweek 2024 Ghent programme overview flyer
pablovgd
 
Remote Sensing and Computational, Evolutionary, Supercomputing, and Intellige...
Remote Sensing and Computational, Evolutionary, Supercomputing, and Intellige...Remote Sensing and Computational, Evolutionary, Supercomputing, and Intellige...
Remote Sensing and Computational, Evolutionary, Supercomputing, and Intellige...
University of Maribor
 
Orion Air Quality Monitoring Systems - CWS
Orion Air Quality Monitoring Systems - CWSOrion Air Quality Monitoring Systems - CWS
Orion Air Quality Monitoring Systems - CWS
Columbia Weather Systems
 
原版制作(carleton毕业证书)卡尔顿大学毕业证硕士文凭原版一模一样
原版制作(carleton毕业证书)卡尔顿大学毕业证硕士文凭原版一模一样原版制作(carleton毕业证书)卡尔顿大学毕业证硕士文凭原版一模一样
原版制作(carleton毕业证书)卡尔顿大学毕业证硕士文凭原版一模一样
yqqaatn0
 

Recently uploaded (20)

Richard's aventures in two entangled wonderlands
Richard's aventures in two entangled wonderlandsRichard's aventures in two entangled wonderlands
Richard's aventures in two entangled wonderlands
 
3D Hybrid PIC simulation of the plasma expansion (ISSS-14)
3D Hybrid PIC simulation of the plasma expansion (ISSS-14)3D Hybrid PIC simulation of the plasma expansion (ISSS-14)
3D Hybrid PIC simulation of the plasma expansion (ISSS-14)
 
bordetella pertussis.................................ppt
bordetella pertussis.................................pptbordetella pertussis.................................ppt
bordetella pertussis.................................ppt
 
Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...
Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...
Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...
 
Seminar of U.V. Spectroscopy by SAMIR PANDA
 Seminar of U.V. Spectroscopy by SAMIR PANDA Seminar of U.V. Spectroscopy by SAMIR PANDA
Seminar of U.V. Spectroscopy by SAMIR PANDA
 
nodule formation by alisha dewangan.pptx
nodule formation by alisha dewangan.pptxnodule formation by alisha dewangan.pptx
nodule formation by alisha dewangan.pptx
 
Introduction to Mean Field Theory(MFT).pptx
Introduction to Mean Field Theory(MFT).pptxIntroduction to Mean Field Theory(MFT).pptx
Introduction to Mean Field Theory(MFT).pptx
 
DERIVATION OF MODIFIED BERNOULLI EQUATION WITH VISCOUS EFFECTS AND TERMINAL V...
DERIVATION OF MODIFIED BERNOULLI EQUATION WITH VISCOUS EFFECTS AND TERMINAL V...DERIVATION OF MODIFIED BERNOULLI EQUATION WITH VISCOUS EFFECTS AND TERMINAL V...
DERIVATION OF MODIFIED BERNOULLI EQUATION WITH VISCOUS EFFECTS AND TERMINAL V...
 
Nucleic Acid-its structural and functional complexity.
Nucleic Acid-its structural and functional complexity.Nucleic Acid-its structural and functional complexity.
Nucleic Acid-its structural and functional complexity.
 
platelets_clotting_biogenesis.clot retractionpptx
platelets_clotting_biogenesis.clot retractionpptxplatelets_clotting_biogenesis.clot retractionpptx
platelets_clotting_biogenesis.clot retractionpptx
 
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
 
The use of Nauplii and metanauplii artemia in aquaculture (brine shrimp).pptx
The use of Nauplii and metanauplii artemia in aquaculture (brine shrimp).pptxThe use of Nauplii and metanauplii artemia in aquaculture (brine shrimp).pptx
The use of Nauplii and metanauplii artemia in aquaculture (brine shrimp).pptx
 
20240520 Planning a Circuit Simulator in JavaScript.pptx
20240520 Planning a Circuit Simulator in JavaScript.pptx20240520 Planning a Circuit Simulator in JavaScript.pptx
20240520 Planning a Circuit Simulator in JavaScript.pptx
 
Deep Behavioral Phenotyping in Systems Neuroscience for Functional Atlasing a...
Deep Behavioral Phenotyping in Systems Neuroscience for Functional Atlasing a...Deep Behavioral Phenotyping in Systems Neuroscience for Functional Atlasing a...
Deep Behavioral Phenotyping in Systems Neuroscience for Functional Atlasing a...
 
What is greenhouse gasses and how many gasses are there to affect the Earth.
What is greenhouse gasses and how many gasses are there to affect the Earth.What is greenhouse gasses and how many gasses are there to affect the Earth.
What is greenhouse gasses and how many gasses are there to affect the Earth.
 
ANAMOLOUS SECONDARY GROWTH IN DICOT ROOTS.pptx
ANAMOLOUS SECONDARY GROWTH IN DICOT ROOTS.pptxANAMOLOUS SECONDARY GROWTH IN DICOT ROOTS.pptx
ANAMOLOUS SECONDARY GROWTH IN DICOT ROOTS.pptx
 
NuGOweek 2024 Ghent programme overview flyer
NuGOweek 2024 Ghent programme overview flyerNuGOweek 2024 Ghent programme overview flyer
NuGOweek 2024 Ghent programme overview flyer
 
Remote Sensing and Computational, Evolutionary, Supercomputing, and Intellige...
Remote Sensing and Computational, Evolutionary, Supercomputing, and Intellige...Remote Sensing and Computational, Evolutionary, Supercomputing, and Intellige...
Remote Sensing and Computational, Evolutionary, Supercomputing, and Intellige...
 
Orion Air Quality Monitoring Systems - CWS
Orion Air Quality Monitoring Systems - CWSOrion Air Quality Monitoring Systems - CWS
Orion Air Quality Monitoring Systems - CWS
 
原版制作(carleton毕业证书)卡尔顿大学毕业证硕士文凭原版一模一样
原版制作(carleton毕业证书)卡尔顿大学毕业证硕士文凭原版一模一样原版制作(carleton毕业证书)卡尔顿大学毕业证硕士文凭原版一模一样
原版制作(carleton毕业证书)卡尔顿大学毕业证硕士文凭原版一模一样
 

Concept of file handling in c

  • 1. MSc. BIOTECHNOLOGY PRESENTED TO: Ms. HIMANI VERMA PRESENTED BY: TANYA(2084043) MUGDHA(2084044) Concept of file handling
  • 2. CONTENT- INTRODUCTION WHAT IS A FILE? TYPES OF FILES NEED OF FILE HANDLING STEPS FOR PROCESSING A FILE FILE INPUT/OUTPUT FUNCTIONS  DECLARATION OF A FILE  OPENING OF A FILE  READING DATA FROM A FILE  WRITING DATA IN A FILE  CLOSING THE FILE PROGRAMS
  • 3. WHAT IS A FILE?  A FILE IS A COLLECTION OF DATA STORED IN ONE UNIT, IDENTIFIED BY A FILENAME.  IT CAN BE A DOCUMENT, PICTURE, AUDIO OR VIDEO STREAM, DATA LIBRARY, APPLICATION, OR OTHER COLLECTION OF DATA.  FILES CAN BE OPENED, SAVED, DELETED, AND MOVED TO DIFFERENT FOLDERS.  THEY CAN ALSO BE TRANSFERRED ACROSS NETWORK CONNECTIONS OR DOWNLOADED FROM THE INTERNET.  A FILES TYPE CAN BE DETERMINED BY VIEWING THE FILES ICON OR BY READING THE FILE EXTENSION.
  • 4. INTRODUCTION  FILE HANDLING IS STORING OF DATA IN A FILE USING A PROGRAM.  WE CAN EXTRACT/ FETCH DATA FROM A FILE TO WORK WITH IT IN THE PROGRAM.  FILES ARE USED TO STORE DATA IN A STORAGE DEVICE PERMANENTLY.  FILE HANDLING PROVIDES A MECHANISMTO STORE THE OUTPUT OF A PROGRAM IN A FILE AND TO PERFORM VARIOUS OPERATIONS ON IT.
  • 5. TYPES OF FILES TEXT FILES - IT IS THE DEFAULT MODE OF A FILE. EACH LINE IN A TEXT FILE IS TERMINATED WITH THE SPECIAL CHARACTER KNOWN AS END OF LINE. THE EXTENSION OF TEXT FILE IS .txt. BINARY FILES – IT CONTAINS SAME FORMAT IN WHICH THE INFORMATION IS HELD IN THE MEMORY. NO TRANSLATIONS ARE REQUIRED IN THIS FILE. CSV FILES – THIS IS THE COMMA SEPARATED VALUES FILE, WHICH ALLOWS DATA TO BE SAVED IN TABULAR FORM. THESE ARE USE TEXT FILES BINARY FILES CSV FILES
  • 6. need of file handling  A FILE IN ITSELF IS A BUNCH OF BYTES STORED ON SOME STORAGE DEVICE.  FILE HELPS US TO STORE DATA PERMANENTLY, WHICH CAN BE RETRIEVED IN FUTURE.  C - LANGUAGE PROVIDES A CONCEPT OF FILE THROUGH WHICH DATA CAN BE STORED ON A DISK OR SECONDARY STORAG DEVICE.  THE STORED DATA CAN BE RETRIEVED WHENEVER NEEDED.  FILE HANDLING IS REQUIREDTO STORE THE OUTPUT OF THE PREOGRAM WHICH CAN BE USED IN THE FUTURE.  NO MATTER WHATEVER APPLICATION WE DEVELOP , WE NEED TO STORE THAT DATA SOMEWHERE, AND HERE DATA HANDLING COMES INTO PLAY.
  • 7. STEPS FOR PROCESSING A FILE  DECLARE A FILE POINTER VARIABLE  OPEN A FILE USING fopen() function  PROCESS THE FILE USING SUITABLE FUNCTION  CLOSE THE FILE USING fclose() function
  • 8. FILE INPUT/OUTPUT FUNCTIONS AVAILABLE IN THE STDIO LIBRARY ARE:
  • 9. DECLARATION OF A FILE POINTER • The type of file that is to be used must be specified • This is accomplished by using a VARIABLE called FILE POINTER (fp) • Pointer variable that points to a structure FILE • The members of the FILE structure are used by the program in various file access operations, but programmers do not need to be concerned about them. • For each file that is to be OPENED, a pointer type FILE must be declared. FILE is a structure declared in stdio.h
  • 10.  When the function fopen() is called, that function creates an instance of the FILE structure and returns a pointer to that structure .  This pointer is used in all subsequent operations on the file.  The SYNTAX for declaring file pointers is as follows; FILE *file_pointer_name,…; FILE *fp;
  • 11. OPENING A FILE: FOR CREATION AND EDIT  Opening a file is performed using the fopen() function defined in the stdio.h header file.  The syntax for opening a file in standard I/O is:  Function fopen takes the name of a file as it's 1st parameter And mode as it's 2nd parameter.  The string given as first parameter for filename, opens the file named and assigns an identifier to the FILE type pointer fp.  This pointer, which contains all the information about the file is subsequently used as a communication link between the system and program.  The string given as second parameter for mode, FILE *fopen("filename","mode");
  • 12. A filename in a C program can also contain path information The path specifies the drive and/or directory or folder name where the file is located. If a filename is specified without a path, it will be assumed that the file is located wherever the operating system currently designates as the default. On PCs, the backslash character ( ) is used to separate directory names in a path. It is to be remembered that the backslash character has a special meaning to C with respect to escape sequence when it is in a string. To represent the backslash character itself, one must precede it with another backslash. Thus, in a C program, the filename would be represented as follows. “c:examdatalist.txt”; Directory name Drive name File name
  • 13. Mode can be one of the following;  If the purpose is " reading" and if it exists, then the file is opened with current contents safe otherwise an error occurs.  When the mode is " writing", a file with the specialised name is created, if the file does not exist. The contents are deleted, if the file already exists.  When the purpose is " appending", the file is opened with the current contents safe. A file with the specified name is created if the file does not exist.
  • 14. These statements are used to create a text file with the name data.dat under current directory It is opened in "w" mode as data are to be written into the file data.dat FILE *fp; fp = fopen("data.dat","w"); Following is an example where a file pointer “ fp” is declared, the file name, which is declared to contain a maximum of 80 characters, is obtained from the keyboard and then the file is opened in the “write” mode. char filename[80]; FILE *fp; printf(“Enter the filename to be opened”); gets(fi lename); fp = fopen(fi lename,“w”);
  • 15. Writing data In a file C provides four functions that can be used to write text file into the disk. These are;  fprintf()  fputs()  fputc()  fwrite() They are just the file versions of printf() puts() putc() write() The only difference is that fprintf() fputs() fputc() fwrite() expects a pointer to the structure FILE.
  • 16. EXAMPLE; Write to a text file #include <stdio.h> #include <stdlib.h> int main() { int num; FILE *fptr fptr = fopen("C:program.txt","w"); if(fptr == NULL) { printf("Error!"); exit(1); } printf("Enter num: "); scanf("%d",&num); fprintf(fptr,"%d",num); fclose(fptr); return 0; }
  • 17. Writing to a binary file To write into a binary file, we need to use the fwrite() function. The functions take 4 arguments: 1. Address of data to be written in the disk 2. Size of data to be written in the disk 3. Number of such type of data 4. Pointer to the file where you want to write fwrite(addressData, sizeData, numbersData, pointerToFile);
  • 18. #include <stdio.h> struct threeNum { int n1, n2, n3; }; int main() { int n; struct threeNum num; FILE *fptr; if ((fptr = fopen("C:program.bin","wb")) == NULL){ printf("Error! opening file"); exit(1); } for(n = 1; n < 5; ++n) { num.n1 = n; num.n2 = 5*n; num.n3 = 5*n + 1; fwrite(&num, sizeof(struct threeNum), 1, fptr); } fclose(fptr); return 0; }
  • 19. Reading data from a file C provides 4 functions that can be used to read text files from the disk  fscanf()  fgets()  fgetc()  fread() These are just the file versions of scanf() gets() getc() and read() The only difference is that fscanf() fgets() fgetc() and fread() expects a pointer to the structural FILE.
  • 20. EXAMPLE: Read from a text file #include <stdio.h> #include <stdlib.h> int main() { int num; FILE *fptr; if ((fptr = fopen("C:program.txt","r")) == NULL){ printf("Error! opening file"); exit(1); } fscanf(fptr,"%d", &num); printf("Value of n=%d", num); fclose(fptr); return 0; }
  • 21. READ FROM A BINARY FILE Function fread() takes 4 arguments: 1. Address of data to be written in the disk 2. Size of data to be written in the disk 3. Number of such type of data 4. Pointer to the file where you want to read fread(addressData, sizeData, numbersData, pointerToFile)
  • 22. Read from a binary file using fread() #include <stdio.h> #include <stdlib.h> struct threeNum { int n1, n2, n3; }; int main() { int n; struct threeNum num; FILE *fptr; if ((fptr = fopen("C:program.bin","rb")) == NULL){ printf("Error! opening file"); exit(1); } for(n = 1; n < 5; ++n) { fread(&num, sizeof(struct threeNum), 1, fptr); printf("n1: %dtn2: %dtn3: %d", num.n1, num.n2, num.n3); } fclose(fptr); return 0: }
  • 23. Closing a file The file (both text and binary) should be closed after readingwriting. Closing a file is performed using the fclose() function. Here, fptr is a file pointer associated with the file to be closed. fclose() returns 0 on success or –1 on error fclose(fptr)
  • 24. When a program terminates (either by reaching the end of main() or by executing the exit() function) All streams are automatically flushed and closed. Actually, in a simple program, it is not necessary to close the file because the system closes all open files before returning to the operating system.
  • 25. programs To open a file, write in it, and close the file To open a file, read from it, and close the file
  • 26. REFERENCES https://www.programiz.com/c-programming/c-file- input-output https://www.geeksforgeeks.org/basics-file-handling- c/ BOOKS:  Programming in C by Pradip Dey and Manas Ghosh  Programming in ANSI by E Balagurusamy  Computer Fundamentals by PK sinha and Priti Sinha
  • 27. This Photo by Unknown author is licensed under CC BY-NC.