Page 302 - DCAP404 _Object Oriented Programming
P. 302
Unit 13: Working with Files
integer type large enough to contain the size of the file. For a file with a size under 2GB we could Notes
use int:
int size;
size = (int) file.tellg();
Once we have obtained the size of the file, we request the allocation of a memory block large
enough to hold the entire file:
memblock = new char[size];
Right after that, we proceed to set the get pointer at the beginning of the file (remember that we
opened the file with this pointer at the end), then read the entire file, and finally close it:
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();
At this point we could operate with the data obtained from the file. Our program simply
announces that the content of the file is in memory and then terminates.
Self Assessment
Fill in the blanks:
15. If you want to enter your own text or data, you will start by ………………….. a file.
16. File streams include two member functions specifically designed to input and output
binary data sequentially: ………………... .
13.7 Command Line Arguments
The main function may be defined not to have any parameter. In some cases, though, the
program is provided with some input values at the time of execution. These values are known
as command line parameters. If the main function must process the command line parameters,
it should be defined as having two parameters – argc of int type and argv of pointer to character
type array.
argc is initialized with a number of parameters provided in command line while argv points to
the list of parameters itself, as is illustrated through the code snippet listed below:
#include<iostream.h>
void main(int argc, char *argv[])
{
for (int i=0; i<argc;i++)
cout<<’\n’<<argv[i];
}
Let us assume that the name of the program is abc.exe. If you execute this program from the
command line as shown below:
C:\>abc delhi agra kolkata
LOVELY PROFESSIONAL UNIVERSITY 295