Chapter-11 OOPC
Chapter-11 OOPC
Weightage: 10%
Hours: 4 Hours needed
5/13/2021 | U & P U. Patel Department of Computer Engineering
Introduction
Many real-time problems, stores large amount of data, which requires hard disk to store the
data.
Such kind of data is stored in the devices using the concept of files.
What is a file?
Programs can be designed to perform read and write operations on these files.
The I/O system of C++ handles file operations which are very much similar to the console input and
output operations.
It uses file streams as an interface between the program and the files.
• Stream that supplies data to the program is known as input stream(reads data from file).
• Stream that receives data from the program is known as output stream(Writes data to file).
5/13/2021 | U & P U. Patel Department of Computer Engineering
File Input and Output Streams
Input stream: The stream that supplies data to the program is known as input stream. It extracts (or read) data
from file.
Output Stream: The stream that receives data from the program is known as output stream. It inserts (or
writes) data to the file.
5/13/2021 | U & P U. Patel Department of Computer Engineering
Classes for file stream operations
When file is opened, object is created for stream associated with it.
Cin, cout is created when <iostream> is included
Allows communication between program and file/device
data file
results file
file-stream-class stream-object;
stream-object.open ( “file_name” );
• Moves the file pointer to the byte number 10. The bytes in a file are numbered
beginning from zero. Therefore, the pointer pointing to the 11th byte in the
file.
ofstream fileout;
fileout.open(“Hello”, ios::app);
int p = fileout.tellp();
• The above statements will represent number of bytes in file.
5/13/2021 | U & P U. Patel Department of Computer Engineering
Seek Function with Specifying the Offset
seekg( offset, refposition);
seekp( offset, refposition);
• The parameter offset represents the number of bytes the file pointer is to be moved from
the location specified by the parameter refposition.
• The refposition takes one of the following three constants defined in the ios class:
• The program request for the stream, and on receiving the stream, it writes character by
character, to the file using put() function in for loop.
• Length of the string is used to terminate the for loop.
• Similarly get function reads the characters from the file, till the end of file is reached.
int good( )
1. argc
2. argv
➢ argv[]: argv[](argument vector) is an array of char type pointers that points to the
command-line-arguments.