0% found this document useful (0 votes)
41 views31 pages

CS201 19

This document discusses random access files in C++. It covers opening and closing files, reading and writing characters and strings to files using functions like get(), put(), read(), write(), as well as getting and setting the file position pointer using functions like tellg(), tellp(), seekg(), seekp(). Examples are provided to demonstrate reading the length of a file, merging files, and writing integers to files in a loop.

Uploaded by

bc200403674
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views31 pages

CS201 19

This document discusses random access files in C++. It covers opening and closing files, reading and writing characters and strings to files using functions like get(), put(), read(), write(), as well as getting and setting the file position pointer using functions like tellg(), tellp(), seekg(), seekp(). Examples are provided to demonstrate reading the length of a file, merging files, and writing integers to files in a loop.

Uploaded by

bc200403674
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 31

Introduction to Programming

Lecture 19
Random Access Files
Files

 open ( file_name , mode ) ;


 close ( ) ;
ifstream myFilePtr ;
myFilePtr.open ( “myFile” , ios :: in ) ;
Output File Stream
 ios :: app
 ios :: trunc
 ios :: ate
Read/write a character

get ( ) Read a character


put ( ) Write a character
Number of Delimiter
characters to read

getline(str,10, ‘\n’) ;
File Positions
File Position Pointer
tellg ( ) Function

myFile.tellg ( ) ;
Returns a whole number which tell you the position
of the next character to be read from the file
tellp ( ) Function
myFile.tellp ( ) ;
Returns a whole number which tell you the position o
the next character to be written in a file
For Positioning in the file

seekg ( ) ;
seekp ( ) ;
seekg ( )
Number of
characters to move Starting point
to

filePtr.seekg ( long Num , ios :: origin ) ;


seekg ( )
seekg ( 10L , ios :: beg ) ;
seekg (10L , ios :: cur ) ;
seekg ( 10L , ios :: end ) ;
Example 1
#include<fstream.h>
main ( )
{
int length ;
ifstream inFile ( “myFile.txt” ) ;
inFile.seekg ( 0L , ios :: end ) ;
length = inFile.tellg ( ) ;
}
File
Name city Date-of-Birth
: : :
Jamil Ahmed Sukkur 10-10-1982
: : :

Rawalpindi
Merge Method
Original file Empty file

This is a text
data And needs
To be replaced NOT
seekg ( )

seekg ( 2201L , ios :: beg ) ;


fstream

fstream myFile ( “Sample.txt” ,


ios :: in | ios :: out ) ;
OR Function
A B Output
0 0 0
0 1 1
1 0 1
1 1 1
Example 2
This is an Apple

This is a Sample
get ( ) and put ( ) character
in a file

myInputFile.get ( c ) ;
myOutputFile.put ( c ) ;
read ( ) and write ( )
Functions
Area in memory
Number of bytes to
be read

read ( char *buff , int count ) ;

Area in memory
Number of bytes to
be written

write ( char *buff , int count ) ;


Example 3
char str [ 10000 ] ;
myInputFile.read ( str , 10000 ) ;
myOuputFile.write ( str , 10000 ) ;
seekg ( )

seekg ( 0L , ios :: end ) ;


seekg ( )

seekg ( -1L , ios :: end ) ;


seekg ( )

seekg ( -2L , ios :: cur ) ;


Address of the
integer ‘i’ Number of bytes
to be written

myOutputFile.write ( &i , 4 ) ;
sizeof ( ) ;
Address of the
integer ‘i’ Size of integer

myOutputFile.write ( &i , sizeof ( i ) ) ;


for ( i = 0 ; i < 100 ; i ++ )
{
myOutputFile.write ( &i , sizeof ( i ) ) ;
}
myOutputFile.close ( ) ;

You might also like