Download presentation
Presentation is loading. Please wait.
1
Sindhu S PGT Comp.Sc. K V Kollam
Data File Handling Sindhu S PGT Comp.Sc. K V Kollam
2
The fstream.h header file
A stream is a general term used to name flow of data. Streams act as an interface between files and programs. A Stream is sequence of bytes. They represent as a sequence of bytes and deals with the flow of data. Every stream is associated with a class having member functions and operations for a particular kind of data flow.
3
FILE USER PROCESS or PROGRAM “write” (Output-ofstream) “cin” (Input) “cout” (Output) “read” (Input-ifstream)
4
If you include fstream.h file in file handling program you need not include iostream.h file as classes of fstream.h inherit from iostream.h. Functions of file stream classes are Filebuf : close() and open() Fstreambase : It is the base class of fstream it provides operations common to these file streams it also contain open() and close()
5
Ifstream class Being an input file stream class, it provides input operations for file. It inherits the functions : a) get() b)getline() c)read() Functions supporting random access: a)seekg() b)tellg()
6
File TYPES A File can be stored in two ways Text File Binary File
Text Files : Stores information in ASCII characters. In text file each line of text is terminated by with special character known as EOL (End of Line) In text file some translations takes place when this EOL character is read or written. Binary File: it contains the information in the same format as it is held in the memory. In binary file there is no delimiter for a line. Also no translation occur in binary file. As a result binary files are faster and easier for program to read and write.
7
Opening a file Using constructor function of the stream class
Eg:-ifstream fin(“Datafile”,ios::in); Using the open() Eg:-ifstream fin; fin.open(“Datafile”,ios::in);
8
Steps in handling a file. 1. Create a file stream. 2. Open a file. 3
Steps in handling a file Create a file stream Open a file Access the file Close the file
9
Closing a File A File is closed by disconnecting it with the stream it is associated with. The close( ) function is used to accomplish this task. Syntax: Stream_object.close( ); Example : fout.close();
10
Input functions Ifstream being an input file stream class, it provides input operations for file. It inherits the functions : a) get() b)getline() c)read() Functions supporting random access: a)seekg() b)tellg()
11
get() – read a single character from text file and store in a buffer.
e.g file.get(ch); Other two forms of get() :- Eg: char line[40]; file.get(line,40,”$”); 2. Eg: char ch; ch=file.get();
12
getline() - read a line of text from text file store in a buffer.
e.g file.getline(s,80,”$”); The difference between get(buf,num,delim) and getline() is that getline() reads and removes the delimiter newline character from the input stream if it is encountered which is not done by the get() function. We can also use file>>ch for reading and file<<ch writing in text file. But >> operator does not accept white spaces.
13
Read Method Syntax: istream & read ((char *) &buf, int sizeof(buf));
File Read data of size ”buf” buf BACK
14
int rollno; char name[25]; }stud={25, “Achu”} ; void main( )
Eg:- struct student { int rollno; char name[25]; }stud={25, “Achu”} ; void main( ) ofstream fout; fout.open(“Stud.dat”,ios::out||ios::binary); fout.write((char*) &stud,sizeof(stud)); fout.close(); ifstream fin; fin.read((char*) &stud,sizeof(stud)); cout<<stud.rno<<stud.name; fin.close(); } BACK
15
File Pointer The file pointer indicates the position in the file at which the next input/output is to occur. Moving the file pointer in a file for various operations viz modification, deletion , searching etc. Following functions are used seekg(): It places the file pointer to the specified position in input mode of file. e.g file.seekg(p,ios::beg); or file.seekg(-p,ios::end), or file.seekg(p,ios::cur) i.e to move to p byte position from beginning, end or current position.
16
tellg(): This function returns the current working position of the file pointer in the input mode.
e.g int p=file.tellg();
17
CBSE QUESTION PATTERN RELATED TO FILE POINTER 1 MARK QNO 4 ( c )
18
CBSE 2015 QP (4(C)) Find the output of the following C++ code considering that the binary file MEMBER.DAT exists on the hard disk with records of 100 members: Class member { Int mno;char name[20]; Public: Void in(); void out(); };
19
} void main() { fstream mf; mf.open(“MEMBER.DAT”,ios::binary|ios::in);
MEMBER M; mf.read((char*)&M,sizeof(M)); Int position=mf.tellg()/sizeof(M); Cout<<“present Record”<<position<<endl; Mf.close(); }
20
OUTPUT Present Record : 3
21
THANK YOU
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.