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
![Page 1: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/1.jpg)
Introduction to ProgrammingIntroduction to Programming
Lecture 19Lecture 19
![Page 2: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/2.jpg)
Random Access FilesRandom Access Files
![Page 3: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/3.jpg)
FilesFiles
open ( file_name , mode ) ;open ( file_name , mode ) ; close ( ) ;close ( ) ;
![Page 4: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/4.jpg)
ifstream myFilePtr ;ifstream myFilePtr ;
myFilePtr.open ( “myFile” , ios :: in ) myFilePtr.open ( “myFile” , ios :: in ) ;;
![Page 5: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/5.jpg)
Output File Output File StreamStream
ios :: appios :: app ios :: ios :: trunctrunc
ios :: ateios :: ate
![Page 6: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/6.jpg)
Read/write a Read/write a charactercharacter
get ( ) get ( ) Read a characterRead a character
put ( ) put ( ) Write a characterWrite a character
![Page 7: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/7.jpg)
getline(str,10, ‘\n’) ;getline(str,10, ‘\n’) ;
Number of characters to read
Delimiter
![Page 8: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/8.jpg)
File File PositionsPositions
![Page 9: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/9.jpg)
File Position PointerFile Position Pointer
![Page 10: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/10.jpg)
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
![Page 11: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/11.jpg)
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
![Page 12: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/12.jpg)
For Positioning in the For Positioning in the filefile
seekg ( ) ;seekg ( ) ;
seekp ( ) ;seekp ( ) ;
![Page 13: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/13.jpg)
seekg ( )seekg ( )
filePtr.seekg ( long Num , ios :: origin ) ;filePtr.seekg ( long Num , ios :: origin ) ;
Number of characters to move
toStarting point
![Page 14: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/14.jpg)
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 ) ;
![Page 15: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/15.jpg)
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 ( ) ;
}}
![Page 16: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/16.jpg)
Name Name city city Date-of-Birth Date-of-Birth
:: : : : :
Jamil AhmedJamil Ahmed Sukkur 10-10-1982Sukkur 10-10-1982
: : :: : :
File
Rawalpindi
![Page 17: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/17.jpg)
Merge MethodMerge Method
This is a text data And needs
Original file Empty file
To be replaced NOT
![Page 18: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/18.jpg)
seekg ( )seekg ( )
seekg ( 2201L , ios :: seekg ( 2201L , ios :: beg ) ;beg ) ;
![Page 19: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/19.jpg)
fstreamfstream
fstream myFile ( “Sample.txt” , fstream myFile ( “Sample.txt” , ios :: in | ios :: out ) ;ios :: in | ios :: out ) ;
![Page 20: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/20.jpg)
OR FunctionOR Function AA B B Output Output 00 0 0 00 00 1 1 11 11 0 0 11 11 1 1 11
![Page 21: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/21.jpg)
Example 2Example 2This is an AppleThis is an Apple
This is a SampleThis is a Sample
![Page 22: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/22.jpg)
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 ) ;
![Page 23: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/23.jpg)
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
![Page 24: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/24.jpg)
char str [ 10000 ] ;char str [ 10000 ] ;
myInputFile.read ( str , 10000 ) ;myInputFile.read ( str , 10000 ) ;
myOuputFile.write ( str , 10000 ) ;myOuputFile.write ( str , 10000 ) ;
Example 3
![Page 25: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/25.jpg)
seekg ( 0L , ios :: seekg ( 0L , ios :: end ) ;end ) ;
seekg ( )seekg ( )
![Page 26: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/26.jpg)
seekg ( )seekg ( )
seekg ( -1L , ios :: seekg ( -1L , ios :: end ) ;end ) ;
![Page 27: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/27.jpg)
seekg ( )seekg ( )
seekg ( -2L , ios :: cur ) ;seekg ( -2L , ios :: cur ) ;
![Page 28: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/28.jpg)
myOutputFile.write ( &i , 4 ) myOutputFile.write ( &i , 4 ) ;;
Address of the integer ‘i’ Number of bytes
to be written
![Page 29: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/29.jpg)
sizeof ( ) sizeof ( ) ;;
![Page 30: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/30.jpg)
myOutputFile.write ( &i , sizeof ( i ) ) ;myOutputFile.write ( &i , sizeof ( i ) ) ;
Address of the integer ‘i’ Size of integer
![Page 31: CS201- Introduction to Programming- Lecture 19](https://reader033.vdocuments.mx/reader033/viewer/2022051514/5492e76fb479596e4f8b4616/html5/thumbnails/31.jpg)
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 ( ) ;