Presentation Transcript
FILE HANDLING IN C++ :FILE HANDLING IN C++
C++ STREAMS :C++ STREAMS A Stream is a general name given to flow of data.
Different streams are used to represent different kinds of data flow.
Each stream is associated with a particular class, which contains member functions and definitions for dealing with that particular kind of data flow.
Flow of Data…. :Flow of Data…. PROGRAM DEVICES OR
FILES Input
Stream
>> Output
Stream
<< Data Data istream class ostream class (Insertion operator) (Extraction
operator)
The following classes in C++ have access to file input and output functions: :The following classes in C++ have access to file input and output functions: ifstream
ofstream
fstream
The Stream Class Hierarchy :The Stream Class Hierarchy ios istream
get()
getline()
read()
>> ostream
put()
write()
<< fstreambase iostream Ifstream
Open()
Tellg()
Seekg() Ofstream
Open()
Tellp()
Seekp() fstream NOTE : UPWARD ARROWS INDICATE
THE BASE CLASS
DIFFERENT FILE OPERATIONS :DIFFERENT FILE OPERATIONS OPENING A FILE
CLOSING A FILE
READING FROM A FILE
WRITING ON A FILE
CHECKING FOR END OF FILE
Slide 7:A binary file stores data to disk in the same form in which it is represented in main memory.
If you ever try to edit a binary file containing numbers you will see that the numbers appear as nonsense characters. Not having to translate numbers into a readable form makes binary files somewhat more efficient.
Binary files also do not normally use anything to separate the data into lines. Such a file is just a stream of data with nothing in particular to separate components.
Slide 8:When using a binary file we write whole record data to the file at once. When using a text file, we write out separately each of the pieces of data about a given record.
The text file will be readable by an editor, but the numbers in the binary file will not be readable in this way.
The programs to create the data files will differ in how they open the file and in how they write to the file.
Slide 9:For the binary file we will use write to write to the file, whereas for the text file we will use the usual output operator(>)
EXAMPLES :EXAMPLES Creation of a text file
:Sequential access. With this type of file access one must read the data in order, much like with a tape, whether the data is really stored on tape or not. Random access (or direct access). This type of file access lets you jump to any location in the file, then to any other, etc., all in a reasonable amount of time. ::Sequential access. With this type of file access one must read the data in order, much like with a tape, whether the data is really stored on tape or not. Random access (or direct access). This type of file access lets you jump to any location in the file, then to any other, etc., all in a reasonable amount of time. Types of File Access
seekg() function : :seekg() function : With one argument :
seekg(k) where k is absolute position from the beginning. The start of the file is byte 0 Begin File End k bytes ^ File pointer The seekg() function with one argument
Slide 13:seekg() function : With two arguments :
the first argument represents an offset from a particular location in the file.
the second specifies the location from which the offset is measured. Begin End ^ Offset from Begin The seekg() function with two argument
Slide 14:seekg() function : With two arguments : Begin End ^ Offset from Begin The seekg() function with two argument ^ ^ Offset from end Offset from current
position