Download presentation
Presentation is loading. Please wait.
1
Chapter 12: File I/O
2
Introduction scanf and printf are the used to read and write date
Console oriented I/O function which use terminal (keyboard and scree) as the target Problem: Cumbersome and time consuming to handle large volume or data Data is lost when program is terminated Solution: Use file to store data Read/Write when necessory
3
File and file operation
File is a place on disk where a group of related data is stored Function for file operation Naming a file Opening file Reading data from file Writing data to a file Closing file
4
Some I/O function for file handling
fopen() and fclose() getc() and putc() fprintf()and fsacnf() getw() and putw() feseek() and ftell() rewind()
5
Stream Any source for input or destination for output
We have used keyboard and screen for small stream But for larger stream we used HD, SSD, CD, DVD or any storage device Stdio.h is common header file to manage these stream
6
Text file VS Binary file
Text file represent a character making it possible for human to examine the file or edit it Example: .txt file, .c file Binary file don’t necessarily represent characters, may represent group of bytes such as integer or floating number. .exe, .dll, .java are such file Text file are divided into line Text file may contain End-of-File marker
7
Opening a File File pointer is created
fopen(“File_name”, “ mode”) used to open file Mode can be Example1
8
Closing File The fclose() function allows a program to close a file that it’s no longer using. The argument fclose() must be a file pointer obtained from a call of fopen()
9
Input/output Operation on file
Once file is open reading out or writing in is accomplished using standard I/O function getc() and putc() are simplest file I/O function Assume file is opened with mode w and file pointer fp1, then putc(c, fp1) writes the character c. Similarly getc() is used to read a character from a file that has been opened in read mode. Ie c=getc(fp2) would read character from fp2 Example 2
10
The getw() and putw() function
Similar to getc() and putc() getw() and putw() are integer-oriented functions Are used to read and write integer value Eg: A file named DATA contains a series of integer number. Code a program read these numbers and then write all odd number to a file called ODD and all even number to a file called EVEN Eg: 3
11
Eg:4 Implementation of Ceasor Cipher
Check example 4
12
fprintf and fscanf function
We have used function that can handle one character or integer at a time fprintf() and fscanf() are function similar to scanf and printf but can works on file. Example 5
13
fwite() function The fwrite() function is used to write records (sequence of bytes) to the file A record may be an array or a structure. Syntax fwrite( ptr, int size, int n, FILE *fp ); The fwrite() function takes four arguments. ptr : ptr is the reference of an array or a structure stored in memory. size : size is the total number of bytes to be written. n : n is number of times a record will be written. FILE* : FILE* is a file where the records will be written in binary mode. Example
14
fwrite() function The fread() function is used to read bytes form the file. Syntax fread( ptr, int size, int n, FILE *fp ); The fread() function takes four arguments. ptr : ptr is the reference of an array or a structure where data will be stored after reading. size : size is the total number of bytes to be read from file. n : n is number of times a record will be read. FILE* : FILE* is a file where the records will be read. Example 8
15
Some More Function rewind() ftell() fseek()
Move the pointer to the beginning of the file Example 9 ftell() Retrive the current position of pointer in the file Example 10 fseek() Move the pointer to the desired position in file Example 11
16
fseek() function Used to move the cursor in the file to the desired position. Eg: 11 Syntax int fseek(FILE *fp, long offset, int pos) The fseek() function takes three arguments, first is the file pointer, second is the offset that specifies the number of bytes to moved and third is the position from where the offset will move. It will return zero if successfully move to the specified position otherwise return nonzero value.
17
The end
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.