File Handling in C plus plus-Object Oriented Programming-Lecture Slides, Slides of Object Oriented Programming

This lecture was delivered by Prof. Usman Younis at Quaid-i-Azam University. This lecture covers following points of course Object Oriented Programming using C plus plus: Operations, Input, Data, Output, File, Stream, Implemented, Object, Operations, Simultaneous

Typology: Slides

2011/2012

Uploaded on 07/31/2012

saqqi
saqqi 🇵🇰

4

(33)

40 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
4/21/2011
1
Ob
j
ect Oriented
j
Programming using C++
Lecture – 12
File Handling in C++
Introduction
So far, we have used the console I/O operations
Input data from the keyboard
Input data from the keyboard
Output data to the screen
What if the data needs to be stored permanently
Data could be transferred to the storage from the
program as files
These files can be text files or binary format
Usman Younis
Output is not lost when the program exits and the
useful information can be accessed for later use
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download File Handling in C plus plus-Object Oriented Programming-Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

Object Orientedj

Programming using C++

Lecture – 12

File Handling in C++

Introduction

„ So far, we have used the console I/O operations

‰‰ Input data from the keyboardInput data from the keyboard

‰ Output data to the screen

„ What if the data needs to be stored permanently

‰ Data could be transferred to the storage from the

program as files

‰ These files can be text files or binary format

Usman Younis

‰ Output is not lost when the program exits and the

useful information can be accessed for later use

File Stream

„ File handling is implemented in C++ using file

streamstream

„ We have already used stream in C++

cin

‰ Input from stream object connected to the keyboard

cout

Output to the stream object connected to the screen

Usman Younis

‰ Output to the stream object connected to the screen

„ Similarly, file stream object is connected to the

storage to perform several file operations

File input and output

Read data from Disk

Input data for processing

Disk

Files

Program

Input file stream

Usman Younis

Output file stream

Output data after processing

Write data to disk

General File I/O Steps

„ Declare a file name variable

„ Associate the file name variable with theh f l bl h h disk file name

„ Open the file

„ Use the file

„ Close the file

Usman Younis

„ Close the file

Files and Streams

„ C++ views file as a sequence of bytes

‰‰ Ends with anEnds with an endend of file-of-file markermarker

„ When file is opened

‰ Object is created and a stream is associated with it „ Communication between program and file/device

0 1 2 3 4 5 8 9 ...

... (^) n- end-of-file marker

6 7

Usman Younis

p g ‰ Similar to cin , cout , which are created when is included

Files and Streams (contd..)

„ C++ imposes no structure on file ‰ Concept of a "record" must be implemented by the programmer „ To open file ‰ create objects ‰ Classes „ ifstream (input only) „ ofstream (output only) „ fstream (I/O) ‰ Constructors take file name and file-open mode

Usman Younis

ofstream outClientFile( "filename", fileOpenMode ); „ To attach a file later ofstream outClientFile; outClientFile.open( "filename", fileOpenMode);

Files and Streams (contd..)

„ File open modes

Mode Result in Open for reading (default for ifstream) out Open for writing (default for ofstream) ate Start reading or writing at end of file (AT End) app Start writing at end of file (APPend) trunc Truncate file to zero length if it exists (TRUNCate) bi O fil i bi ( ) d

Usman Younis

ofstream outClientFile( "clients.dat", ios::out ); ofstream outClientFile( "clients.dat"); //ofstream opened for output by default

binary Open file in binary (non-text) mode

Output using sequential access file

int account; char name[30]; char ch = 'y'; double balance;

//read account, name and balance from cin, then place in file while (ch == 'y') { cin>>account; cin>>name; cin>>balance; outFile<<account<<" "<<name<<' '<<balance<<endl;

cout<<"Add another (y/n)?";

Usman Younis

(y ) ; cin>>ch; } //ofstream destructor closes file //or you can do it manually outFile.close(); } // end main

Output using sequential access file

outfile.txt

2345 Taz 56789. 2347 Gurp 7896. 6789 Ozzy 8907. 6754 Moss 8765.

Usman Younis

Input using sequential access file

#include <iostream.h> #include <fstream.h> #include <process.h>

void main() { //ifstream constructor opens file ifstream inFile("outfile.txt", ios::in);

if(!inFile) //overloaded! operator {

Usman Younis

cout<<"Unable to open the file"<<endl;

//exit program if unable to create file exit(1); }

Input using sequential access file

int account; char name[30]; char ch = 'y'; double balance;double balance; //read account, name and balance from inFile, then display while (!inFile.eof()) { inFile>>account>>name>>balance;

cout<<account<<" "<<name<<' '<<balance<<endl; } // end while

Usman Younis

} // end while

//ifstream destructor closes file //or you can do it manually inFile.close(); } // end main

Reading data from a sequential

access file

„ To find pointer locationTo find pointer location

‰ tellg and tellp ‰ location = fileObject.tellg()

Usman Younis

Storage in sequential access file

„ "1234567" (character array) vs 1234567 (int)

‰‰ char array takes 8 bytes (1 for each character + null)char array takes 8 bytes (1 for each character + null) ‰ int takes fixed number of bytes (4) „ 123 same size in bytes as 1234567

„ << operator in outFile

‰ outFile << number

Usman Younis

‰ Outputs number (int) as a character array ‰ Variable number of bytes

„ Updating sequential files

Risk of overwriting other data

Updating sequential access files

„ Risk of overwriting other data

„ Example: change name “John" to “John Roberts"

‰ Old data „ 300 John 400 32. ‰ Insert new data 300 John Roberts

Usman Younis

300 John 400 32.

300 John Roberts

300 John Roberts

Data gets overwritten

Random Access Files

Please see the uploaded code