File handling in C by Faixan
-
Upload
faixy- -
Category
Technology
-
view
543 -
download
0
Transcript of File handling in C by Faixan
Files in C-Language
Outlines
• Basic of file • Types of file • Creating a file • Input/ Output to a file• Streams associated with files
• A file is a collection of bytes stored on a secondary storage device.
• A file represents a sequence of bytes on the disk where a group of related data is stored.
• File is created for permanent storage of data. It is a readymade structure.
Basic of file
Continued• In C language, we use a structure pointer of
file type to declare a file. FILE *fp;• C provides a number of functions that helps to
perform basic file operations. Following are the functions:
Continued
Function Descriptionfopen() create a new file or open a existing filefclose() closes a filegetc() reads a character from a fileputc() writes a character to a filefscanf() reads a set of data from a filefprintf() writes a set of data to a filegetw() reads a integer from a fileputw() writes a integer to a file
Continued
• General Syntax : fp=fopen(“file_name.extansion”,”mode”)
Types of file
• A file is classified into two types:
• Binary file:- • If a large amount of numerical data it to be
stored, text mode will be insufficient. In such case binary file is used.
Continued
• Working of binary files is similar to text files with few differences in opening modes, reading from file and writing to file.
• Opening modes of binary files:• Opening modes of binary files are rb, rb+, wb,
wb+,ab and ab+. The only difference between opening modes of text and binary files is that, b is appended to indicate that, it is binary file.
Continued
• Reading and writing of a binary file:• Functions fread() and fwrite() are used for
reading from and writing to a file on the disk respectively in case of binary files.
Continued
• ASCII Text file:-• In this type we use fopen() for open or create a
file and fclose() for closing the file. In Text file the modes are “r”, “w”, “a” and “r+”.
• It is opened for one kind of operation (reading, writing, or appending) at any give time.
• We can read only one character at a time from a text file.
Creating a file
• Writing to a file:- Opening or creating a file is performed using library function fopen().
Continued• Example:-
Continued
• Output:-
Continue• Reading to a file:-
Continued
• Output:-
Input/ Output to a file
• In the above table we have discussed about various file I/O functions to perform reading and writing on file.
• getc() and putc() are simplest functions used to read and write individual characters to a file.
Continued• Input to a file:-
Continued
Continued• Output to a file:-
Continued
Streams associated with files
• A stream refers to the flow of data (in bytes) from one place to another (from program to file or vice-versa).
• There are two types of streams:1. Text Stream2. Binary Stream
Continued
• Text Stream:-• It consists of sequence of characters.• Each line of characters in the stream may be
terminated by a newline character. • Text streams are used for textual data, which
has a consistent appearance from one environment to another or from one machine to another.
Continued
• Binary Stream:-• It is a series of bytes. • Binary streams are primarily used for non-
textual data, which is required to keep exact contents of the file.
Continued
Data Type Descriptionifstream open the file for input
ofstream open the file for output
fstream open the file for input/output/both
ContinuedMode Flag Description
ios::app Append mode. All output to that file to be appended to the end.
ios::ate Open a file for output and move the read/write control to the end of the file.
ios::in Open a file for reading.
ios::out Open a file for writing.
ios::trunc If the file already exists, its contents will be truncated before opening the file.
ios::binary Binary file
ios::nocreate do not create the file,open only if it exists
ios::noreplace open and create a new file if the specified file does not exist
Continued• Example:-
Continued
Conclusion
• A file is a collection of bytes stored on a secondary storage device.
• How to create a file in c program.• Different mode in file and different functions
in file.• How to create a stream file.
Thank you… Prepared by: Muhammad Faizan Akhter (BSCS-15104) Department of CS and IT, Superior University Lahore.