SlideShare una empresa de Scribd logo
1 de 27
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.

Más contenido relacionado

La actualidad más candente

La actualidad más candente (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 a Concept of file handling in c

Similar a 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
 

Último

PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...Sérgio Sacani
 
Botany 4th semester series (krishna).pdf
Botany 4th semester series (krishna).pdfBotany 4th semester series (krishna).pdf
Botany 4th semester series (krishna).pdfSumit Kumar yadav
 
Orientation, design and principles of polyhouse
Orientation, design and principles of polyhouseOrientation, design and principles of polyhouse
Orientation, design and principles of polyhousejana861314
 
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Sérgio Sacani
 
Chromatin Structure | EUCHROMATIN | HETEROCHROMATIN
Chromatin Structure | EUCHROMATIN | HETEROCHROMATINChromatin Structure | EUCHROMATIN | HETEROCHROMATIN
Chromatin Structure | EUCHROMATIN | HETEROCHROMATINsankalpkumarsahoo174
 
DIFFERENCE IN BACK CROSS AND TEST CROSS
DIFFERENCE IN  BACK CROSS AND TEST CROSSDIFFERENCE IN  BACK CROSS AND TEST CROSS
DIFFERENCE IN BACK CROSS AND TEST CROSSLeenakshiTyagi
 
Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)PraveenaKalaiselvan1
 
Green chemistry and Sustainable development.pptx
Green chemistry  and Sustainable development.pptxGreen chemistry  and Sustainable development.pptx
Green chemistry and Sustainable development.pptxRajatChauhan518211
 
Formation of low mass protostars and their circumstellar disks
Formation of low mass protostars and their circumstellar disksFormation of low mass protostars and their circumstellar disks
Formation of low mass protostars and their circumstellar disksSérgio Sacani
 
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...anilsa9823
 
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...jana861314
 
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCESTERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCEPRINCE C P
 
Disentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTDisentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTSérgio Sacani
 
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral AnalysisRaman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral AnalysisDiwakar Mishra
 
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...Sérgio Sacani
 
GBSN - Biochemistry (Unit 1)
GBSN - Biochemistry (Unit 1)GBSN - Biochemistry (Unit 1)
GBSN - Biochemistry (Unit 1)Areesha Ahmad
 
GFP in rDNA Technology (Biotechnology).pptx
GFP in rDNA Technology (Biotechnology).pptxGFP in rDNA Technology (Biotechnology).pptx
GFP in rDNA Technology (Biotechnology).pptxAleenaTreesaSaji
 
Hire 💕 9907093804 Hooghly Call Girls Service Call Girls Agency
Hire 💕 9907093804 Hooghly Call Girls Service Call Girls AgencyHire 💕 9907093804 Hooghly Call Girls Service Call Girls Agency
Hire 💕 9907093804 Hooghly Call Girls Service Call Girls AgencySheetal Arora
 

Último (20)

PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
 
Botany 4th semester series (krishna).pdf
Botany 4th semester series (krishna).pdfBotany 4th semester series (krishna).pdf
Botany 4th semester series (krishna).pdf
 
Orientation, design and principles of polyhouse
Orientation, design and principles of polyhouseOrientation, design and principles of polyhouse
Orientation, design and principles of polyhouse
 
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
 
Chromatin Structure | EUCHROMATIN | HETEROCHROMATIN
Chromatin Structure | EUCHROMATIN | HETEROCHROMATINChromatin Structure | EUCHROMATIN | HETEROCHROMATIN
Chromatin Structure | EUCHROMATIN | HETEROCHROMATIN
 
DIFFERENCE IN BACK CROSS AND TEST CROSS
DIFFERENCE IN  BACK CROSS AND TEST CROSSDIFFERENCE IN  BACK CROSS AND TEST CROSS
DIFFERENCE IN BACK CROSS AND TEST CROSS
 
Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)
 
Green chemistry and Sustainable development.pptx
Green chemistry  and Sustainable development.pptxGreen chemistry  and Sustainable development.pptx
Green chemistry and Sustainable development.pptx
 
Formation of low mass protostars and their circumstellar disks
Formation of low mass protostars and their circumstellar disksFormation of low mass protostars and their circumstellar disks
Formation of low mass protostars and their circumstellar disks
 
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
 
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
 
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCESTERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
 
Disentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTDisentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOST
 
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral AnalysisRaman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
 
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
 
GBSN - Biochemistry (Unit 1)
GBSN - Biochemistry (Unit 1)GBSN - Biochemistry (Unit 1)
GBSN - Biochemistry (Unit 1)
 
GFP in rDNA Technology (Biotechnology).pptx
GFP in rDNA Technology (Biotechnology).pptxGFP in rDNA Technology (Biotechnology).pptx
GFP in rDNA Technology (Biotechnology).pptx
 
The Philosophy of Science
The Philosophy of ScienceThe Philosophy of Science
The Philosophy of Science
 
CELL -Structural and Functional unit of life.pdf
CELL -Structural and Functional unit of life.pdfCELL -Structural and Functional unit of life.pdf
CELL -Structural and Functional unit of life.pdf
 
Hire 💕 9907093804 Hooghly Call Girls Service Call Girls Agency
Hire 💕 9907093804 Hooghly Call Girls Service Call Girls AgencyHire 💕 9907093804 Hooghly Call Girls Service Call Girls Agency
Hire 💕 9907093804 Hooghly Call Girls Service Call Girls Agency
 

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.