SEM1-BPL - Lesson13-File Handling
SEM1-BPL - Lesson13-File Handling
LESSON 13
File Handling
CONTENT
1. File I/O Concepts
2. Working with Streams
• Text Stream
• Binary Stream
3. I/O Functions:
• open(), close()
• fread(), fwrite(), fseek()
• fscanf(), fprintf()
4. Summary
Basic Programming Language 2
Why Files Are Needed?
• When a program is terminated, the entire data is lost. Storing in a file will
preserve your data even if the program terminates.
• If you have to enter a large number of data, it will take a lot of time to enter
them all.
However, if you have a file containing all the data, you can easily access
the contents of the file using a few commands in C.
• You can easily move your data from one computer to another without any
changes.
• Close File:
• Using fclose() function
• File Manipulation:
• Using fread() functions read from file
• Using fwrite() functions write to file
• Close File:
• Using fclose() function
• Different from opening a text file is adding parameter “b” in opening mode.
• The fflush() function flushes out the buffer depending upon the file type.
• A file opened for read will have its input buffer cleared, while a file opened
for write will have its output buffer written to the files.
• The prototype for fflush() function is:
int fflush(FILE *fp);
• The fflush() function, with a null, flushes all files opened for output.