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

An introduction to file input and output (i/o) operations in c++ using docsity.com. Topics covered include opening and closing files, reading and writing characters using get() and put(), file positions using tellg() and tellp(), and positioning using seekg() and seekp(). Examples are given to illustrate the use of these functions.

Typology: Slides

2011/2012

Uploaded on 11/06/2012

somo
somo 🇮🇳

4.8

(4)

70 documents

1 / 30

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to Programming
Lecture 19
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e

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