cs201- introduction to programming- lecture 19
DESCRIPTION
Virtual University Course CS201- Introduction to Programming Lecture No 19 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]TRANSCRIPT
Introduction to ProgrammingIntroduction to Programming
Lecture 19Lecture 19
Random Access FilesRandom Access Files
FilesFiles
open ( file_name , mode ) ;open ( file_name , mode ) ; close ( ) ;close ( ) ;
ifstream myFilePtr ;ifstream myFilePtr ;
myFilePtr.open ( “myFile” , ios :: in ) myFilePtr.open ( “myFile” , ios :: in ) ;;
Output File Output File StreamStream
ios :: appios :: app ios :: ios :: trunctrunc
ios :: ateios :: ate
Read/write a Read/write a charactercharacter
get ( ) get ( ) Read a characterRead a character
put ( ) put ( ) Write a characterWrite a character
getline(str,10, ‘\n’) ;getline(str,10, ‘\n’) ;
Number of characters to read
Delimiter
File File PositionsPositions
File Position PointerFile Position Pointer
tellg ( ) tellg ( ) FunctionFunction
myFile.tellg ( ) ;myFile.tellg ( ) ;Returns a whole number which tell you the positionReturns a whole number which tell you the position
of the next character to be read from the fileof the next character to be read from the file
tellp ( ) tellp ( ) FunctionFunction
myFile.tellp ( ) ;myFile.tellp ( ) ;Returns a whole number which tell you the position Returns a whole number which tell you the position
of the next character to be written in a fileof the next character to be written in a file
For Positioning in the For Positioning in the filefile
seekg ( ) ;seekg ( ) ;
seekp ( ) ;seekp ( ) ;
seekg ( )seekg ( )
filePtr.seekg ( long Num , ios :: origin ) ;filePtr.seekg ( long Num , ios :: origin ) ;
Number of characters to move
toStarting point
seekg ( )seekg ( ) seekg ( 10L , ios :: seekg ( 10L , ios ::
beg ) ;beg ) ;
seekg (10L , ios :: cur ) ;seekg (10L , ios :: cur ) ;
seekg ( 10L , ios :: seekg ( 10L , ios :: end ) ;end ) ;
Example 1Example 1#include<fstream.h>#include<fstream.h>main ( )main ( ){{
int length ;int length ; ifstream inFile ( “myFile.txt” ) ifstream inFile ( “myFile.txt” ) ;; inFile.seekg ( 0L , ios :: end ) ;inFile.seekg ( 0L , ios :: end ) ; length = inFile.tellg ( ) ;length = inFile.tellg ( ) ;
}}
Name Name city city Date-of-Birth Date-of-Birth
:: : : : :
Jamil AhmedJamil Ahmed Sukkur 10-10-1982Sukkur 10-10-1982
: : :: : :
File
Rawalpindi
Merge MethodMerge Method
This is a text data And needs
Original file Empty file
To be replaced NOT
seekg ( )seekg ( )
seekg ( 2201L , ios :: seekg ( 2201L , ios :: beg ) ;beg ) ;
fstreamfstream
fstream myFile ( “Sample.txt” , fstream myFile ( “Sample.txt” , ios :: in | ios :: out ) ;ios :: in | ios :: out ) ;
OR FunctionOR Function AA B B Output Output 00 0 0 00 00 1 1 11 11 0 0 11 11 1 1 11
Example 2Example 2This is an AppleThis is an Apple
This is a SampleThis is a Sample
get ( ) and put ( ) character get ( ) and put ( ) character in a filein a file
myInputFile.get ( c ) ;myInputFile.get ( c ) ;
myOutputFile.put myOutputFile.put ( c ) ;( c ) ;
read ( ) and write ( ) read ( ) and write ( ) FunctionsFunctions
read ( read ( char *buff , int count char *buff , int count ) ;) ;
write ( write ( char *buff , int count char *buff , int count ) ;) ;
Area in memoryNumber of bytes to
be read
Area in memoryNumber of bytes to
be written
char str [ 10000 ] ;char str [ 10000 ] ;
myInputFile.read ( str , 10000 ) ;myInputFile.read ( str , 10000 ) ;
myOuputFile.write ( str , 10000 ) ;myOuputFile.write ( str , 10000 ) ;
Example 3
seekg ( 0L , ios :: seekg ( 0L , ios :: end ) ;end ) ;
seekg ( )seekg ( )
seekg ( )seekg ( )
seekg ( -1L , ios :: seekg ( -1L , ios :: end ) ;end ) ;
seekg ( )seekg ( )
seekg ( -2L , ios :: cur ) ;seekg ( -2L , ios :: cur ) ;
myOutputFile.write ( &i , 4 ) myOutputFile.write ( &i , 4 ) ;;
Address of the integer ‘i’ Number of bytes
to be written
sizeof ( ) sizeof ( ) ;;
myOutputFile.write ( &i , sizeof ( i ) ) ;myOutputFile.write ( &i , sizeof ( i ) ) ;
Address of the integer ‘i’ Size of integer
for ( i = 0 ; i < 100 ; i ++ )for ( i = 0 ; i < 100 ; i ++ )
{{
myOutputFile.write ( &i , sizeof ( i ) ) ;myOutputFile.write ( &i , sizeof ( i ) ) ;
}}
myOutputFile.close ( ) ;myOutputFile.close ( ) ;