SlideShare a Scribd company logo
For more Https://www.ThesisScientist.com
Unit 10
File Handling
What is a File?
Wherever there is a need to handle large volumes of data, it is advantageous to store data on the disks and
read whenever necessary. This method employs the concept of files to store data. A file is a place on disk
where a group of related data is stored. C supports a number of functions that have the ability to perform
basic file operations, which include:
 naming a file
 opening a file
 reading data from a file
 writing data to a file
 closing a file
There are two distinct ways to perform file operations in C.
1. Low level I/O Operation (It uses operating system calls)
2. High level I/O Operation (It uses functions in C's Standard I/O library)
Table 10.1: List of High Level I/O Functions
Function Name Operation
fopen() Creates a new file for use or opens an existing file
for use.
fclose() Closes a file which has been opened for use.
getc() Reads a character from a file.
putc() Writes a character to a file.
fprintf() Writes a set of data values to a file.
fscanf() Reads a set of data values from a file.
getw() Reads an integer from a file.
putw() Writes an integer to a file.
Defining and Opening a File
Following is the general format for declaring and opening a file:
File *fp;
fp = fopen ( "filename", "mode");
The first statement declares the variable fp as a "pointer to the data type FILE".
The second statement opens the file named filename 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 the program.
The second statement also specifies the purpose of opening this file. The mode does this job.
Mode can be one of the following:
r Opens the file for reading only.
w Opens the file for writing only.
a Opens the file for appending (or adding) data to it.
Both the filename and mode are specified as string. They should be enclosed in double quotation marks.
Depending on the mode specified, one of the following actions may be performed:
1. When the mode is 'writing', a file with the specified name is created, if the file does not exist. The
contents are deleted, if the file already exists.
2. 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.
3. If the purpose is 'reading', and if it exists, then the file is opened with the current contents safe,
otherwise an error occurs.
Closing a File
Once all the operations on a file have been completed, the file is closed. This is done to clear the buffers
and flush all the information associated with the file. It also prevents any accidental misuse of the file. In
case there is a limit to the number of files that can be kept open simultaneously, closing of unwanted files
might help open the required files. When there is a need to use a file in a different mode, the file has to be
first closed and then reopened in a different mode.
The I/O library supports a function for this of the following form:
fclose (file_pointer);
This would close the file associated with the file pointer file_pointer.
e.g.: .........
FILE *p1, *p2;
p1 = fopen ("INPUT", "w");
p2 = fopen ("OUTPUT", "r");
.........
fclose(p1);
fclose(p2);
This program opens two files and closes them after all operations on them are completed.
Input/Output Operations on Files
getc & putc Functions
These are analogous to getchar and putchar functions and can handle only one character at a time.
putc can be used to write a character in a file opened in write mode.
A statement like putc (ch, fp1); writes the character contained in the character variable ch to the file
associated with file pointer fp1.
Similarly, getc is used to read a character from a file that has been opened in read mode.
The statement c = getc(fp2); would read a character from the file whose file pointer is fp2.
The file pointer moves by one character position for every operation of getc or putc. The getc will return an
end-of-file marker EOF, when end of the file has been reached. The reading should be terminated when
EOF is encountered. Testing for the end-of-file condition is important. Any attempt to read past the end of
file might either cause the program to terminate with an error or result in an infinite loop situation.
getw & putw Functions
The getw and putw are integer-oriented functions. They are similar to the getc and putc functions and are
used to read and write integer values on UNIX systems.
The general forms of getw and putw are:
putw (integer, fp); & getw (fp);
fprintf & fscanf Functions
The functions fprintf and fscanf perform I/O operations that are identical to the familiar printf and scanf
functions.
The general syntax of fprintf is
fprintf (fp, "control string", list);
where fp is a file pointer associated with a file that has been opened for writing. The control string contains
output specifications for items in the list. The list may include variables, constants and strings.
e.g.: fprintf (f1,"%s %d %f", name, age, 7.5); here name is an array variable of type char and age in an
int variable.
The general syntax of fscanf is
fscanf (fp, "control string", list);
This statement would cause the reading of the items in the list from the file specified by fp, according to the
specifications contained in the control string.
e.g.: fscanf (f2, "%s %d", item, &quantity);

More Related Content

PDF
File_Management_in_C
PPTX
file management in c language
PPTX
File Management in C
PPTX
File management in C++
PPTX
Programming in C
PPTX
Programming in C Session 4
PPT
File Management
PPTX
File Management in C
File_Management_in_C
file management in c language
File Management in C
File management in C++
Programming in C
Programming in C Session 4
File Management
File Management in C

What's hot (20)

PPTX
File handling in C by Faixan
DOCX
Understanding c file handling functions with examples
PDF
File Management
DOCX
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
PPTX
File handling in c
PDF
Files in C
PPTX
File in C language
PPT
File in c
PPT
File handling-c programming language
PPTX
File Handling and Command Line Arguments in C
PPTX
C Programming Unit-5
PPTX
File handling in C
PPT
File handling in c
PPT
File handling in c
PPT
Mesics lecture files in 'c'
PPT
File in C Programming
PDF
4 text file
PPT
Lecture 20 - File Handling
File handling in C by Faixan
Understanding c file handling functions with examples
File Management
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
File handling in c
Files in C
File in C language
File in c
File handling-c programming language
File Handling and Command Line Arguments in C
C Programming Unit-5
File handling in C
File handling in c
File handling in c
Mesics lecture files in 'c'
File in C Programming
4 text file
Lecture 20 - File Handling
Ad

Similar to File handling C program (20)

PPTX
Unit-VI.pptx
PDF
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
PDF
FILES IN C
PPTX
File management
PPTX
File management
DOCX
PPTX
INput output stream in ccP Full Detail.pptx
PPTX
File handling in c
PDF
VIT351 Software Development VI Unit5
PDF
Handout#01
PPTX
18CS56-UP-Module 3.pptx
PPTX
File Handling
PPTX
File Handling
PPTX
PPS PPT 2.pptx
PPT
Unit5
PPTX
Topic - File operation.pptx
PPTX
File handling in C hhsjsjshsjjsjsjs.pptx
PPT
file_handling_in_c.ppt
PDF
23CS101T PSPP python program - UNIT 5.pdf
PPT
file_handling_in_c.ppt
Unit-VI.pptx
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
FILES IN C
File management
File management
INput output stream in ccP Full Detail.pptx
File handling in c
VIT351 Software Development VI Unit5
Handout#01
18CS56-UP-Module 3.pptx
File Handling
File Handling
PPS PPT 2.pptx
Unit5
Topic - File operation.pptx
File handling in C hhsjsjshsjjsjsjs.pptx
file_handling_in_c.ppt
23CS101T PSPP python program - UNIT 5.pdf
file_handling_in_c.ppt
Ad

More from Thesis Scientist Private Limited (20)

PDF
HTML guide for beginners
PDF
Ransomware attacks 2017
PDF
How to write a Great Research Paper?
PDF
Research Process design
PDF
How to write a good Dissertation/ Thesis
PDF
How to write a Research Paper
PDF
Internet security tips for Businesses
PDF
How to deal with a Compulsive liar
PDF
Driverless car Google
PDF
Podcast tips beginners
PDF
Vastu for Career Success
PDF
Reliance jio broadband
PDF
Job Satisfaction definition
PDF
Mistakes in Advertising
PDF
Contributor in a sentence
PDF
Different Routing protocols
PDF
Ad hoc network routing protocols
PDF
Latest Thesis Topics for Fog computing
PDF
Latest Research Topics On Flying Ad-Hoc Networks (FANETs):
HTML guide for beginners
Ransomware attacks 2017
How to write a Great Research Paper?
Research Process design
How to write a good Dissertation/ Thesis
How to write a Research Paper
Internet security tips for Businesses
How to deal with a Compulsive liar
Driverless car Google
Podcast tips beginners
Vastu for Career Success
Reliance jio broadband
Job Satisfaction definition
Mistakes in Advertising
Contributor in a sentence
Different Routing protocols
Ad hoc network routing protocols
Latest Thesis Topics for Fog computing
Latest Research Topics On Flying Ad-Hoc Networks (FANETs):

Recently uploaded (20)

PDF
Tree Biomechanics, a concise presentation
PPTX
Disposal Of Wastes.pptx according to community medicine
PPTX
Green Modern Sustainable Living Nature Presentation_20250226_230231_0000.pptx
PPTX
Arugula. Crop used for medical plant in kurdistant
PPTX
UN Environmental Inventory User Training 2021.pptx
PPTX
Plant_Cell_Presentation.pptx.com learning purpose
DOCX
Epoxy Coated Steel Bolted Tanks for Dairy Farm Water Ensures Clean Water for ...
PDF
The Truth Behind Vantara zoo in Jamnagar
PPTX
Biodiversity.udfnfndrijfreniufrnsiufnriufrenfuiernfuire
PDF
The Role of Non-Legal Advocates in Fighting Social Injustice.pdf
PDF
Urban Hub 50: Spirits of Place - & the Souls' of Places
PPTX
Envrironmental Ethics: issues and possible solution
PDF
Blue Economy Development Framework for Indonesias Economic Transformation.pdf
PDF
Bai bao Minh chứng sk2-DBTrong-003757.pdf
PDF
Effect of anthropisation and revegetation efforts on soil bacterial community...
DOCX
Epoxy Coated Steel Bolted Tanks for Anaerobic Digestion (AD) Plants Core Comp...
PDF
School Leaders Revised Training Module, SCB.pdf
PPTX
Delivery census may 2025.pptxMNNN HJTDV U
PPT
Compliance Monitoring report CMR presentation.ppt
PPTX
structure and components of Environment.pptx
Tree Biomechanics, a concise presentation
Disposal Of Wastes.pptx according to community medicine
Green Modern Sustainable Living Nature Presentation_20250226_230231_0000.pptx
Arugula. Crop used for medical plant in kurdistant
UN Environmental Inventory User Training 2021.pptx
Plant_Cell_Presentation.pptx.com learning purpose
Epoxy Coated Steel Bolted Tanks for Dairy Farm Water Ensures Clean Water for ...
The Truth Behind Vantara zoo in Jamnagar
Biodiversity.udfnfndrijfreniufrnsiufnriufrenfuiernfuire
The Role of Non-Legal Advocates in Fighting Social Injustice.pdf
Urban Hub 50: Spirits of Place - & the Souls' of Places
Envrironmental Ethics: issues and possible solution
Blue Economy Development Framework for Indonesias Economic Transformation.pdf
Bai bao Minh chứng sk2-DBTrong-003757.pdf
Effect of anthropisation and revegetation efforts on soil bacterial community...
Epoxy Coated Steel Bolted Tanks for Anaerobic Digestion (AD) Plants Core Comp...
School Leaders Revised Training Module, SCB.pdf
Delivery census may 2025.pptxMNNN HJTDV U
Compliance Monitoring report CMR presentation.ppt
structure and components of Environment.pptx

File handling C program

  • 1. For more Https://www.ThesisScientist.com Unit 10 File Handling What is a File? Wherever there is a need to handle large volumes of data, it is advantageous to store data on the disks and read whenever necessary. This method employs the concept of files to store data. A file is a place on disk where a group of related data is stored. C supports a number of functions that have the ability to perform basic file operations, which include:  naming a file  opening a file  reading data from a file  writing data to a file  closing a file There are two distinct ways to perform file operations in C. 1. Low level I/O Operation (It uses operating system calls) 2. High level I/O Operation (It uses functions in C's Standard I/O library) Table 10.1: List of High Level I/O Functions Function Name Operation fopen() Creates a new file for use or opens an existing file for use. fclose() Closes a file which has been opened for use. getc() Reads a character from a file. putc() Writes a character to a file. fprintf() Writes a set of data values to a file. fscanf() Reads a set of data values from a file. getw() Reads an integer from a file. putw() Writes an integer to a file. Defining and Opening a File Following is the general format for declaring and opening a file: File *fp; fp = fopen ( "filename", "mode"); The first statement declares the variable fp as a "pointer to the data type FILE".
  • 2. The second statement opens the file named filename 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 the program. The second statement also specifies the purpose of opening this file. The mode does this job. Mode can be one of the following: r Opens the file for reading only. w Opens the file for writing only. a Opens the file for appending (or adding) data to it. Both the filename and mode are specified as string. They should be enclosed in double quotation marks. Depending on the mode specified, one of the following actions may be performed: 1. When the mode is 'writing', a file with the specified name is created, if the file does not exist. The contents are deleted, if the file already exists. 2. 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. 3. If the purpose is 'reading', and if it exists, then the file is opened with the current contents safe, otherwise an error occurs. Closing a File Once all the operations on a file have been completed, the file is closed. This is done to clear the buffers and flush all the information associated with the file. It also prevents any accidental misuse of the file. In case there is a limit to the number of files that can be kept open simultaneously, closing of unwanted files might help open the required files. When there is a need to use a file in a different mode, the file has to be first closed and then reopened in a different mode. The I/O library supports a function for this of the following form: fclose (file_pointer); This would close the file associated with the file pointer file_pointer. e.g.: ......... FILE *p1, *p2; p1 = fopen ("INPUT", "w"); p2 = fopen ("OUTPUT", "r"); ......... fclose(p1); fclose(p2);
  • 3. This program opens two files and closes them after all operations on them are completed. Input/Output Operations on Files getc & putc Functions These are analogous to getchar and putchar functions and can handle only one character at a time. putc can be used to write a character in a file opened in write mode. A statement like putc (ch, fp1); writes the character contained in the character variable ch to the file associated with file pointer fp1. Similarly, getc is used to read a character from a file that has been opened in read mode. The statement c = getc(fp2); would read a character from the file whose file pointer is fp2. The file pointer moves by one character position for every operation of getc or putc. The getc will return an end-of-file marker EOF, when end of the file has been reached. The reading should be terminated when EOF is encountered. Testing for the end-of-file condition is important. Any attempt to read past the end of file might either cause the program to terminate with an error or result in an infinite loop situation. getw & putw Functions The getw and putw are integer-oriented functions. They are similar to the getc and putc functions and are used to read and write integer values on UNIX systems. The general forms of getw and putw are: putw (integer, fp); & getw (fp); fprintf & fscanf Functions The functions fprintf and fscanf perform I/O operations that are identical to the familiar printf and scanf functions. The general syntax of fprintf is fprintf (fp, "control string", list); where fp is a file pointer associated with a file that has been opened for writing. The control string contains output specifications for items in the list. The list may include variables, constants and strings. e.g.: fprintf (f1,"%s %d %f", name, age, 7.5); here name is an array variable of type char and age in an int variable. The general syntax of fscanf is fscanf (fp, "control string", list); This statement would cause the reading of the items in the list from the file specified by fp, according to the specifications contained in the control string. e.g.: fscanf (f2, "%s %d", item, &quantity);