0% found this document useful (0 votes)
232 views

Object Oriented Programming: File Processing in C++

This document discusses file processing in C++. It covers: 1) Opening files for input/output using ifstream, ofstream and fstream classes. Common modes include ios::in, ios::out, ios::app. 2) Performing formatted I/O to files using insertion/extraction operators << and >> to write/read basic data types like int, double, strings. 3) Examples are provided to demonstrate opening files, writing data to files, and reading data from files.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
232 views

Object Oriented Programming: File Processing in C++

This document discusses file processing in C++. It covers: 1) Opening files for input/output using ifstream, ofstream and fstream classes. Common modes include ios::in, ios::out, ios::app. 2) Performing formatted I/O to files using insertion/extraction operators << and >> to write/read basic data types like int, double, strings. 3) Examples are provided to demonstrate opening files, writing data to files, and reading data from files.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Object Oriented Programming

CS212

File Processing in C++


Lecture Content

 Text 1- Chapter 14 File Processing

2 CS212 Object Oriented Programming in C++


File Processing

• Storage of data in memory is temporary.

• Files are used for data persistence—permanent


retention of data.

• Computers store files on secondary storage devices,


such as hard disks, CDs, DVDs, flash drives and
tapes.

3 CS212 Object Oriented Programming in C++


File Processing

4 CS212 Object Oriented Programming in C++


1. Write data(i.e Transfer data from Program to File
2. Read data(i.e. bring stored data from file to Program)

5 CS212 Object Oriented Programming in C++


Files in C
 The old C functions, such as fread() and fwrite(), will
still work in C++, but they are not so well suited to the
object-oriented environment.
 The new C++ approach is considerably cleaner and
easier to implement

6 CS212 Object Oriented Programming in C++


Files in C++
 Working with disk files requires the following classes
 fstream for both reading and writing files.
 ifstream for input (reading files)
 ofstream for output (writing files)

7 CS212 Object Oriented Programming in C++


Files in C++
 fstream is derived from iostream,
 ifstream is derived from istream,
 ofstream is derived from ostream.
 The file-oriented classes are also derived, by multiple
inheritance, from the fstreambase class.
 The ifstream, ofstream, and fstream classes are
declared in the FSTREAM file.
 You need to include both <iostream> and <fstream>
headers in your program for file IO.

8 CS212 Object Oriented Programming in C++


File Output
The steps are:
1. Construct an ostream object.

2. Connect it to a file (i.e., file open/constructor) and set the


mode of file operation

3. Perform output operation via << operator


or functions( write(), put() ).
4. Disconnect (close the file which flushes the output buffer)
and free the ostream object

9 CS212 Object Oriented Programming in C++


Example 1

10 CS212 Object Oriented Programming in C++


Example 2

11 CS212 Object Oriented Programming in C++


Open a File
 The first operation generally performed on an object of
one of these classes is to associate it to a real file. This
procedure is known as to open a file.

 By default, opening an output file creates a new file if


the filename does not exist; or truncates it (clear its
content) and starts writing as an empty file.

12 CS212 Object Oriented Programming in C++


Open a File : Mode
ios::in Open for input operations.
ios::out Open for output operations.
ios::binary Open in binary mode.
Set the initial position at the end of the file.
ios::ate If this flag is not set, the initial position is the
beginning of the file.
All output operations are performed at the end
ios::app of the file, appending the content to the current
content of the file.
If the file is opened for output operations and it
ios::trunc already existed, its previous content is deleted
13 and replaced
CS212 Object by theProgramming
Oriented new one. in C++
Formatted File I/O
 In formatted I/O, numbers are stored on disk as a
series of characters.
 Thus 6.02, rather than being stored as a 4-byte type
float or an 8-byte type double, is stored as the
characters ‘6’, ‘.’, ‘0’, and ‘2’.
 This can be inefficient for numbers with many digits,
but it’s appropriate in many situations and easy to
implement.
 Characters and strings are stored more or less
normally.

14 CS212 Object Oriented Programming in C++


Open a File:Mode

class default mode parameter


ofstream ios::out
ifstream ios::in
fstream ios::in | ios::out
All these flags can be combined using the bitwise
operator OR (|).

15 CS212 Object Oriented Programming in C++


Example 3

16 CS212 Object Oriented Programming in C++


Output
 The program writes a character, an integer, a type
double, and two string objects to a disk file.

File Written
x 77 6.02 alpha beta

17 CS212 Object Oriented Programming in C++


Example 4

18 CS212 Object Oriented Programming in C++


Example 5
 #include<iostream>
 #include<fstream>
 using namespace std;
 int main() {
 ofstream myfile(“abc.txt”); bool is_open (); // Returns
 if (myfile.is_open()) { true if the file is successfully
 myfile << “File Created\n”; opened
 myfile.close();
 }
 else {
 cout << “Error! Opening File” << endl;
 }

19 CS212 Object Oriented Programming in C++


File Input
The steps are:
1. Construct an ifstream object.

2. Connect it to a file (i.e., file open/constructor) and set the


mode of file operation

3. Perform input operation via >> operator


or functions(read(), get(), getline() functions.)
4. Disconnect (close the file which flushes the output buffer)
and free the ostream object

20 CS212 Object Oriented Programming in C++


Example 6

21 CS212 Object Oriented Programming in C++


Example 7

22 CS212 Object Oriented Programming in C++


Example 8

23 CS212 Object Oriented Programming in C++

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy