0% found this document useful (0 votes)
21 views9 pages

Yeswanth CP

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views9 pages

Yeswanth CP

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

File Handling in C

Efficiently managing and manipulating files is a crucial skill for any C


programmer. From opening and reading to writing and closing, this guide
will explore the essential aspects of file handling in the C programming
language.
Opening Files

1 fopen() 2 File Modes 3 Error Handling


The primary Common file Always check if
function for modes include "r" the file was
opening files in C for reading, "w" opened
is fopen(). It for writing, and successfully, as
allows you to "a" for appending fopen() can
specify the file data to the file. return NULL if an
name and mode error occurs.
of operation.
R eading and Writing F iles
R eading Files Writing Files Buffering

Use functions like E mploy fwrite() and Utilize buffering to


fread() and fscanf() to fprintf() to write data to improve file I/O
read data from a file. a file. performance and
reduce system calls.
File Modes
"r" "w"
Open file for reading. Open file for writing, creating a
new file if it doesn't exist.

"a" "r+"
Open file for appending, adding Open file for both reading and writing.
data to the end of the file.
File Pointers
End-of-File
Current Position The feof() function can be used to
The file pointer keeps track of the check if the file pointer has
current position within the file. reached the end of the file.

1 2 3

Manipulating Position
Use functions like fseek() and
ftell() to move the file pointer to a
specific location.
F ile Handling F unctions

fopen() fread() fwrite() fclose()


Open a file R ead data from a file Write data to a file Close a file
Closing Files
Cleanup
Closing files is essential to ensure data integrity and release system resources.

fclose()
The fclose() function is used to close a file that was previously opened with fopen().

Error Handling
Always check the return value of fclose() to ensure the file was closed successfully.
THANK YOU

You might also like