Docsity

C++ File I/O: Read/Write Characters, Positioning, Stream Manipulation, Slides of Computer Programming

Partial preview of the text

Download C++ File I/O: Read/Write Characters, Positioning, Stream Manipulation and more Slides Computer Programming in PDF only on Docsity!

Introduction to Programming Lecture 19

Random Access

Files

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

Output File Stream

 ios :: app  ios :: trunc  ios :: ate

File Positions

File Position

Pointer

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

For Positioning in the file seekg ( ) ; seekp ( ) ;

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

Merge Method This is a text data And needs Original file Empty file To be replaced (^) NOT

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

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