31
Introduction to Programming Introduction to Programming Lecture 19 Lecture 19

CS201- Introduction to Programming- Lecture 19

Embed Size (px)

DESCRIPTION

Virtual University Course CS201- Introduction to Programming Lecture No 19 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]

Citation preview

Page 1: CS201- Introduction to Programming- Lecture 19

Introduction to ProgrammingIntroduction to Programming

Lecture 19Lecture 19

Page 2: CS201- Introduction to Programming- Lecture 19

Random Access FilesRandom Access Files

Page 3: CS201- Introduction to Programming- Lecture 19

FilesFiles

open ( file_name , mode ) ;open ( file_name , mode ) ; close ( ) ;close ( ) ;

Page 4: CS201- Introduction to Programming- Lecture 19

ifstream myFilePtr ;ifstream myFilePtr ;

myFilePtr.open ( “myFile” , ios :: in ) myFilePtr.open ( “myFile” , ios :: in ) ;;

Page 5: CS201- Introduction to Programming- Lecture 19

Output File Output File StreamStream

ios :: appios :: app ios :: ios :: trunctrunc

ios :: ateios :: ate

Page 6: CS201- Introduction to Programming- Lecture 19

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

getline(str,10, ‘\n’) ;getline(str,10, ‘\n’) ;

Number of characters to read

Delimiter

Page 8: CS201- Introduction to Programming- Lecture 19

File File PositionsPositions

Page 9: CS201- Introduction to Programming- Lecture 19

File Position PointerFile Position Pointer

Page 10: CS201- Introduction to Programming- Lecture 19

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

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

For Positioning in the For Positioning in the filefile

seekg ( ) ;seekg ( ) ;

seekp ( ) ;seekp ( ) ;

Page 13: CS201- Introduction to Programming- Lecture 19

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

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

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

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

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

seekg ( )seekg ( )

seekg ( 2201L , ios :: seekg ( 2201L , ios :: beg ) ;beg ) ;

Page 19: CS201- Introduction to Programming- Lecture 19

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

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

Example 2Example 2This is an AppleThis is an Apple

This is a SampleThis is a Sample

Page 22: CS201- Introduction to Programming- Lecture 19

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

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

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

seekg ( 0L , ios :: seekg ( 0L , ios :: end ) ;end ) ;

seekg ( )seekg ( )

Page 26: CS201- Introduction to Programming- Lecture 19

seekg ( )seekg ( )

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

Page 27: CS201- Introduction to Programming- Lecture 19

seekg ( )seekg ( )

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

Page 28: CS201- Introduction to Programming- Lecture 19

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

sizeof ( ) sizeof ( ) ;;

Page 30: CS201- Introduction to Programming- Lecture 19

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

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 ( ) ;