Dedicated to My mother and Father
Keep your notebook with you.
Write important point and questions that comes in your mind
Solve Mind band exercise.
Rewind when not clear
Ask Questions by call or SMS or by mail
Keep Watching Keep Learning
THIS IS FILE I/O
• Files are used to store data to disks.
• Data on disk is permanent.
• Variables are stored on RAM only.
• Data on RAM can not be accessed later.
• File : Group of related records.
Sonu 001 04/02
Amit 002 09/05
Asif 003 10/09
Hari 004 30/11
Sonu 001 04/02 Record
Files in C
• A file is a sequence of bytes.
– File ends with End Of File (EOF) marker.
• Streams are used to communicate with file.
• Use FILE pointer to access a file.
• fopen() library function is used to open a file.
– fopen( file_path, open_mode);
– Returns NULL if file can not open.
• fclose() library function is used to close a file.
File open mode
r Open a file for reading.
w Create a file for writing. If the file already exists, discard the current contents.
a Append; open or create a file for writing at end of file.
r+ Open a file for update (reading and writing).
w+ Create a file for update. If the file already exists, discard the current contents.
a+ Append; open or create a file for update; writing is done at the end of the file.
rb Open a file for reading in binary mode.
wb Create a file for writing in binary mode. If the file already exists, discard the current contents.
ab Append; open or create a file for writing at end of file in binary mode.
rb+ Open a file for update (reading and writing) in binary mode.
wb+ Create a file for update in binary mode. If the file already exists, discard the current contents.
ab+ Append; open or create a file for update in binary mode; writing is done at the end of the file.
Read / Write a file
• fgetc() : reads one character from a file.
• fputc() : write one character to a file.
• fgets() : read a line (Srring) from a file.
• fputs() : Write a line to a file.
• fprintf() : used to print to a file.
• fscanf() : used to read from a file.
• feof() : return true if EOF find.
• rewind() : set file position pointer to the start.
• Write a program to read a file and print it on
• Write a program to create a copy of a file.
• Modify first problem: stop printing if a ‘z’
occurs in file.
• Write a program to create a file.
Sequential Access File
• Write or Read data sequentially.
• Data can not modify without destroying.
• Fields can vary in size.
Random Access File
• Access any record without searching through
• Data can be inserted without destroying other
• Implemented using fixed length records.
• Also known as binary file.
Random Access File…
• fwrite() : used to write to binary file.
• fread() : read from binary file.
• fseek(): Used to set the file position pointer to
a specific location.
• Operation sequention.
fwrite() / fread()
• data : data that you want to write to file.
• sizeof(data) : number of bytes to write.
• 1 : for arrays, number of elements to write.
• fp : file to write.
• &data : variable to store data.
• sizeof(data) : number of bytes to read.
• 1 : for arrays, number of elements to read.
• fp : file from read.
– fp : File pointer.
– offset : location of file position pointer.
– symbolic_constant : specify where in file we are
• SEEK_SET: beginning of file.
• SEEK_CUR: current location in file.
• SEEK_END: end of file.
To get complete benefit of this tutorial solve all the quiz on
For any problem in this tutorial mail me at
with the subject “C”
For Other information mail at
Keep Watching Keep Learning
NOW YOU ARE READY TO LEARN C++ AND JAVA