Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 36

Presentation On

DATA FILE
HANDLING
Presented by :--
Naveen Verma
INTRODUCTION
File Handling is made up of two
different words…….

FILE HANDLING FILE HANDLING

File is a collection of bytes or data.


Handling is the way of organizing ,
and maintaining file to get full
advantage of it…..
 Most computer programs
work with files.
 This is because files help in

storing information
permanently.
 For eg– Word processor

creats Document files.


 Compiler reads source file &

creats executable files.


FILE
A FILE is itself a bunch
of bytes stored on some
storage device like Tape
or Magnetis Tape.
STREAM
A STREAM is a sequence of
bytes.
Or
A Stream is a flow of bytes into or
out of program.
Generally 3 Streams are used for I/O
of a file :-
IFSTREAM
OFSTREAM
FSTREAM
 IFSTREAM == Ties file to Input
buffer so that the file can be
read from.
 OFSTREAM == Ties file to

Output buffer so that the file can


be writtn onto.
 FSTREAM == Ties file to buffer

so that the file can be read from


as well as written onto.
Input-Output Stream For Files

INPUT STREAM
Read data from Disk INPUT DATA

Disk Program
OUTPUT STREAM
File

OUTPUT DATA
Write data to Disk
File Input and Output using Streams
ios

istream streambuf ostream

iostream

ifstream fstream ofstream filebuf

fstreambase
DATA FILES

 The data files are the files


that store data pertaining
to a specific application for
later use.
DATA FILE

TEXT FILE BINARY FILE


TEXT FILE BINARY FILE
 Matter saved is in  Matter saved is in
ASCII code. Binary code.
 Easily Readable.  Not-Easily Readable.
 Accessing is slow.  Accessing is fast.
 EOL(end of line) is  EOL(end of line) is
used. not used.
 Internal translations  NO Internal
take place. translations take
place.
 Security level is low.  Security level is high.
File Operations
 Creation of a new file
 Opening of an existing file

 Reading from a file

 Writing to a file

 Moving to a specific location in a

file.
 Closing a file.
OPENING OF FILES
 For opening you must first choose
the stream .
 There are 3 streams.

(ifstream,ofstream,fstream)
Opening of file is of 2 ways :---
1) Using constructor function.

2) Using the function open( ).


Using Constructor
Syntax :::
Stream object(“File name”);
Example==
ifstream Nav(“Naveen.txt”);
We defined --
1)ifstream as stream.
2)Nav as object (user defined).
3)File Naveen.txt to be opened for input
i.e for reading only.
Block Diagram
DISK
INPUT STREAM

DATA
FILE Nav

Program
When we use a file pointer to traverse
the file then we use function --
fopen( )
Syntax ::--::>
fopen(“File name”,”file mode”);

Example
FILE *fp;
fp=fopen(“Naveen.Txt”,”r”);
fopen( ) performes 3 main functions :-

1) Search for file on disk to be opened.

2) Then loads the file from disk into a


place in memory called buffer.

3) It sets up a character pointer that


points to the first character of the
buffer.
FILE MODES
fopen(“Naveen.txt”,”------”)
there are various but explaining some –
1) (r)  searches file
 opens file
 loads to memory & set pointer
to 1st character.
Not opened then returns null.
2) (w)  searches file
 if found ,contents are overwritten.
 if file not found new file is created.
 Writing is possible.
3) (a)  same as (r)
 sets pointer to last character.
 If dosen’t exists then new file is
created.
4) (r+) searches file
 opens file if found or
give null.
Loads to memory.
Reads existing contents.
writing new content.
Modifying existing
contents.
5) (w+)
 searches file
 opens file found or give null.
Loads to memory.
writing new content.
reading them back.
Modifying existing contents.
6) (a+)
 searches file
 opens file found or give null.
Loads to memory.
reading existing content.
Appending new content at end of
file.
Cannot modify existing contents.
Closing the File
When we are finished with the operation
on the file we are adviced to close the
file.This is being done by the fclose( )
function.
fclose(fp);
Once the file is closed we are no longer
there to read data from the file until we
reopen it.
When we close the file following 3
things occurs……..

(a) The character in the buffer would be


written to the file on the disk.

(b) At the end of the file a character with


ASCII code 26 will be written.

(c) The buffer would be eliminated from the


memory.
Some Important functions
used in Data File handling.
 Fprintf( )==same as cout.
 Fscanf( )==same as cin.
 Fread()
 Fwrite(&e , sizeof(e),1,fp);
 Fgets(s,size,fp);
 Fputs()== to put something in file i.e
character or string.
Rewind()==This places the file
pointer to the beginning of the
file.
Fseek()== This lets you to move
the pointer from one record to
another from current position.

 SEEK_END==means move
pointer from end of the file.
SEEK_CUR==means move pointer
with reference to its current
position.
SEEK_SET==means move pointer
with reference to the beginning of
file.
Ftell()==tells u the current position
of pointer.
i.e-- position=ftell(fp);
Program to dislpay
characters of your text
file in any directory to
console……..
#include <stdio.h>
void main()
{
FILE *fopen(), *fp;
int c ;

fp = fopen( "D:/prog.txt", "r" );


c = getc( fp ) ;
while ( c != EOF )
{
putchar( c );
c = getc ( fp );
}

fclose( fp );
getch();
}
Program to get record
from keyboard and then
write them on the file in
binary mode.
#include "stdio.h"
Void main( )
{
FILE *fp ;
char another = 'Y' ;
struct emp
{
char name[40] ;
int age ;
float bs ;
};
struct emp e ;
fp = fopen ( "EMP.DAT", "wb" ) ;
if ( fp == NULL )
{
puts ( "Cannot open file" ) ;
exit( ) ;
}
while ( another == 'Y' )
{
printf ( "\nEnter name, age and basic salary: " ) ;
scanf ( "%s %d %f", e.name, &e.age, &e.bs ) ;
fwrite ( &e, sizeof ( e ), 1, fp ) ;
printf ( "Add another record (Y/N) " ) ;

fflush ( stdin ) ;
another = getche( ) ;
}
fclose ( fp ) ;
}
THANK YOU VERY
MUCH
&
SORRY FOR MY
FAULTS
By :--
N@VEEN VERMA

You might also like