File handling in C by Faixan

29
Files in C-Language

Transcript of File handling in C by Faixan

Page 1: File handling in C by Faixan

Files in C-Language

Page 2: File handling in C by Faixan

Outlines

• Basic of file • Types of file • Creating a file • Input/ Output to a file• Streams associated with files

Page 3: File handling in C by Faixan

• 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

Page 4: File handling in C by Faixan

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:

Page 5: File handling in C by Faixan

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

Page 6: File handling in C by Faixan

Continued

• General Syntax : fp=fopen(“file_name.extansion”,”mode”)

Page 7: File handling in C by Faixan

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.

Page 8: File handling in C by Faixan

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.

Page 9: File handling in C by Faixan

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.

Page 10: File handling in C by Faixan

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.

Page 11: File handling in C by Faixan

Creating a file

• Writing to a file:- Opening or creating a file is performed using library function fopen().

Page 12: File handling in C by Faixan

Continued• Example:-

Page 13: File handling in C by Faixan

Continued

• Output:-

Page 14: File handling in C by Faixan

Continue• Reading to a file:-

Page 15: File handling in C by Faixan

Continued

• Output:-

Page 16: File handling in C by Faixan

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.

Page 17: File handling in C by Faixan

Continued• Input to a file:-

Page 18: File handling in C by Faixan

Continued

Page 19: File handling in C by Faixan

Continued• Output to a file:-

Page 20: File handling in C by Faixan

Continued

Page 21: File handling in C by Faixan

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

Page 22: File handling in C by Faixan

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.

Page 23: File handling in C by Faixan

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.

Page 24: File handling in C by Faixan

Continued

Data Type Descriptionifstream open the file for input

ofstream open the file for output

fstream open the file for input/output/both

Page 25: File handling in C by Faixan

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

Page 26: File handling in C by Faixan

Continued• Example:-

Page 27: File handling in C by Faixan

Continued

Page 28: File handling in C by Faixan

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.

Page 29: File handling in C by Faixan

Thank you… Prepared by: Muhammad Faizan Akhter (BSCS-15104) Department of CS and IT, Superior University Lahore.