Docsity

File Handling in Object-Oriented Programming using C++, Slides of C programming

Partial preview of the text

Download File Handling in Object-Oriented Programming using C++ and more Slides C programming in PDF only on Docsity!

Asif Nawaz University Institute of Information Technology, PMAS-AAUR

Object-Oriented Programming in C++

File Management

Lesson No. 10

Asif Nawaz University Institute of Information Technology, PMAS-AAUR

What is File?

• Files in C++ are interpreted as a sequence of

bytes stored on some storage media.

• The data of a file is stored in either readable

form or in binary code called as text file or

binary file.

• Computer programs are associated to work

with files as it helps in storing data &

information permanently.

Asif Nawaz University Institute of Information Technology, PMAS-AAUR

Streams

  • (^) Stream(Flow of data from source to a destination).
    • (^) a channel where data are passed to receivers from senders.
  • (^) Output Stream
    • (^) a channel where data are sent out to a receiver
    • (^) cout; the standard output stream (to monitor)
    • (^) the monitor is a destination device
  • (^) Input Stream
    • (^) a channel where data are received from a sender
    • (^) cin; the standard input stream (from the keyboard)
    • (^) the keyboard is a source device

Asif Nawaz University Institute of Information Technology, PMAS-AAUR

Standard Input/Output Stream

• Standard Streams use #include <iostream.h>

  • (^) Standard Input Stream
    • (^) cin names the stream
    • (^) the extractor operator >> extracts, gets, or receives the next element from the stream
  • (^) Standard Output Stream
    • (^) cout names the stream
    • (^) the inserter operator << inserts, puts, or sends the next element to the stream
  • (^) Opening & closing of the standard streams occur

automatically when the program begins & ends.

Asif Nawaz University Institute of Information Technology, PMAS-AAUR Mr. Dave Clausen 7

File Streams

  • (^) Data structures that are stored separately

from the program (using auxiliary memory)

  • (^) Streams must be connected to these files
  • (^) Input File Stream
    • (^) extracts, receives, or gets data from the file
  • (^) Output File Stream
    • (^) inserts, sends, or puts data to the file
  • (^) #include <fstream.h> creates two new classes
    • (^) ofstream ( o utput f ile stream)
    • (^) ifstream ( i nput f ile stream)

Asif Nawaz University Institute of Information Technology, PMAS-AAUR

Opening a File

Before data can be written to or read from a

file, the file must be opened.

ifstream inputFile;

inputFile.open(“customer.dat”);

Note:

A file is opened for output for writing data into it and is also opened for input for reading data from it. Similarly, a file can also be opened fro both input and output operations.

Asif Nawaz University Institute of Information Technology, PMAS-AAUR

File Operation Modes

• The data files can be opened into different

modes.

  • (^) These modes are defined in “ ios” class.
  • (^) The resolution operator (::) is used between ios and mode code.

Asif Nawaz University Institute of Information Technology, PMAS-AAUR

File Operation Modes

Name Description ios::in Open file to read ios::out Open file to write ios::app All the date you write, is put at the end of the file. It calls ios::out ios::ate Moves the file pointer to the end of file when the file is opened. ios::trunc Deletes all previous content in the file. (empties the file) ios::nocreate If the file does not exist, opening it with the open() function gets impossible. ios::noreplace If the file exists, trying to open it with the open() function, returns an error. ios::binary Opens the file in binary mode.

Asif Nawaz University Institute of Information Technology, PMAS-AAUR

Verifying File Open

• Syntax

if(!Test)

cout<<“Error in opening File”;

Asif Nawaz University Institute of Information Technology, PMAS-AAUR

File Open Mode

#include <fstream.h> main() { ofstream outFile("file1.txt", ios::out); outFile << "That's new!\n"; outFile.close(); } If you want to set more than one open mode, just use the OR operator- |. This way: ios::ate | ios::binary

Asif Nawaz University Institute of Information Technology, PMAS-AAUR

File I/O Example: Writing

#include <fstream.h> #include <fstream.h> main( ) main( ) { { ofstream file(“c:\fout.txt"); ofstream file(“c:\fout.txt"); file << "Hello World!"; file << "Hello World!"; fout.close(); fout.close(); return 0; return 0; } }

Asif Nawaz University Institute of Information Technology, PMAS-AAUR

Program that write name and ages of ten

persons into a data file on the disk.

Asif Nawaz University Institute of Information Technology, PMAS-AAUR

Program that return data from formatted I/O

File.

Asif Nawaz University Institute of Information Technology, PMAS-AAUR