0 ratings0% found this document useful (0 votes) 11 views8 pagesFile Handing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
2
9
Page 2 /9
File handling (only if time is available, otherwise should be
done as part of the lab)
File handling in programming refers to the process of reading from and
\arting to fles on 2 computer's storage. This allows programs to interact
with extemal data, store Information persistently, and share deta
between different executions of the program. File handling operations
typlealy involve opening files, reading or writing data, and closing fles.
Here are key concepts related to fle handling:
Drag from top and touch the back button to
exit full screen.
© Scanned with OKEN ScannerPage 3 /9 He @ Q@ « >
1, File Operations:
Opening a File:
Use functions like f open to open a file for reading, writing, or both.
Closing a File:
Use f close to close an open file, ensuring that resources are released.
2. File Modes:
Files can be opened in different modes:
“r': Read mode
rite mode (creates a new file or truncates an existing one)
Append mode (writes to the end of the file)
Binary mode (for handling binary files)
"+": Read and write mode
© Scanned with OKEN ScannerPage 4 mo fl we @ Q@ < >
3. Reading from a File:
Use functions lke f scan f or f gets to read data from a fle
The f get c function reads a single character.
4. Writing to a File:
Use functions lke f print for f puts to write data to a file.
‘The fputc function writes a single character.
5, Binary File Handling:
For handling binary files, use functions lke f read and f waite
6. Error Handling:
Check the return values of fle handling functions for errors,
Use functions tke fe of to check for the end ofa file
© Scanned with OKEN ScannerPage 5 /9 AF @ A < >
Z. File Positionina:
Use functions Ike f seek and f tell to set and get the file position,
8. Text Vs. Binary Files:
Text files store data as human-readable text.
Binary files store data in a format that Is not human-readable, allowing
for more efficient storage of complex data structures.
9. File Streams:
Files are typically accessed through fle streams, such as FILE
structures in languages ike C.
File Handling is the storing of data in a file using a program. In
CC programming language, the programs store results, and
other data of the program to a file using file handling in C. Also,
we can extracvfetch data from a file to work with It In the
program.
The operations that you can perform on a File in C are ~
+ Creating a new file
‘© Opening an existing file
Reading data from an existing file
Writing data to a file
Moving data to a specific location on the file
© Scanned with OKEN ScannerPage 6 /9 Hove Q@ QA « >
+ Closing the file
Creating Or Opening File Using F Open
The f open () function is used to create a new file or open an
existing file in C. The f open function is defined in the stdio.h
header file.
Now, lets see the syntax for creation of a new fle or opening a
file
file = f open(file _ name", “mode")
This is a common syntax for both opening and creating a file in
Cc.
Parameters
File _name - Itis a string that specifies the name of the file that
is to be opened o created using the f open method, Mode : It is
a string (usually a single character ) that specifies the mode in
which the file is to be opened. There are various modes
available to open a file in C, we will learn about all of them later
in this article.
When Will A File Be Created?
© Scanned with OKEN Scanner2
2
Page 7 Jo Q
The f open function will create a new file when it will not find
any file of the specified name in the specified location. Else, if
the file is found it will be opened with the mode specified.
Let’s see can example which will make the concept clear,
Suppose we are opening a file named [Link] using the f open
function. The following will be the statement,
file = f open(‘[Link]’, w")
This will search for a file named [Link] in the current
directory. If the file exists, it will open the file otherwise it wil
create a new file named “[Link]" and open it with write mode
(specified using *w’)
Now, let's see all types of modes that are available for us to
read or write a file in C, and see code snippets that will show
sample runs of the code.
Mode = ‘r" - open for reading, this mode will open the file for
reading purpose only, i.e. the contents can only be viewed,
nothing else like edits can be done to it.
This mode cannot create a new file and open () retums NULL, if
we try to create a new file using this mode.
© Scanned with OKEN Scanner#include
Puen
oma
FILE *sourceFile, *destinationFile;
cr
sourceFile = fopen(
it (sourceFile == NULL) {
Diced ag
ae eam Vik oH
Clete se eee
Beale eC see eee
oeaag
Beate cere
return EXIT_FAILURE;
cen,
while ((ch = fgete(sourceFile)) != EOF) (
sted cme Ula CO
© Scanned with OKEN Scannerfclose(sourceFile) ;
Brae 80-1808
pana
The program opens a source file ("[Link]") in read mode and a
destination file ("[Link]’) in write mode
It reads characters from the source file one by one and writes them
to the destination file.
The f get c function is used to read characters, and f put c is used
to write characters.
The loop continues until the end of the file (EOF) is reached
After copying is complete, both files are closed
© Scanned with OKEN Scanner