POS/370
Programming
Concepts Using C++
Week 5
Newsletter (E. Nikjeh)
Hello Again,
Storage of data in variables and arrays is temporary. Files are used for permanent retention of large amounts of data. Computers store files on secondary storage device such as magnetic disks, optical disks and tapes.
Record: A record (i.e., a struct in C++) is one or more related
field (called members in C++). For example, a record for a student might
consist of the following fields:
1- Student ID
number
2- Student Name
3- Address
4- GPA
File: A collection of related
records is called a data file. A student file for a first grade elementary
school might contain only 18 records, whereas a student file for a university
might contain 200,000 records. To facilitate the retrieval of specific records
from a file, at least one field in each record is chosen as a record key. A
record key identifies a record, as belonging to a particular person or entity
that is distinct from all other records in the file. There are many ways to
organize records in a file:
1- Sequential file: It is similar to cassette
tapes (VCR tapes) in that each record in the file, like each song, is both
stored and retrieved in consecutive order (sequentially). So we cannot jump to
the middle of the file (tape).
2- Random
file: Sequential access files are inappropriate for so-called instant
access applications in which a particular record of information must be located
immediately. For example, airline reservation systems, banking systems,
automated teller machines and other kinds of transaction processing systems
that require rapid access to specific data. Individual records of a random
access file can be accessed directly (and quickly) without searching through
other records. Data can be inserted, updated, or deleted without rewriting the
entire file.
3- Binary file: There will be times when
you need to store unformatted (raw) binary data, not text. The ios::binary value causes a file to be
opened in binary mode. By default all files are opened in text mode. Binary
files contain a sequence of bytes with a one-to-one correspondence to the
sequence found in the external device (disk, tape, or terminal). In binary
file, no character translations will occur. For this reason, the number of
bytes read or written will be the same as that found in the external device.
When an application is developed that needs to read an executable file, the
file should be read as a binary file e.g. databases. The data in a binary
access file can be accessed by its byte location in the file.
Sequential File
Processing: To
create a file object, you must include the fstream
header file in the program. #include<fstream>
The fstream header file contains the definitions of the ifstream (input file stream) and ofstream (output file stream) classes that allow you to create input and output file objects. ifstream inFile; // It creates inFile as an input file
ofstream outFile; // It creates outFile
as an output file.
We can only read from inFile and we cannot write
anything to it. We can only write to outFile and we cannot read it.
In order to open inFile as an input we should have:
inFile.open(“a:\sales.dat”, ios::in);
// ios::in is optional for input
So it opens inFile with the name sales.dat in a drive (floppy disk).
To open outFile for output we should have:
outFile.open(“payroll.dat”, ios::out);
// ios::out is optional for output
To open a file for both input and output, we should
use ios::io so we can read from and
write to it.
If we want to add some data to an existing file we
should use ios::app which app stands for append.
There is a built in function (is_open()) which we can check if we opened our file properly. if(outFile.is_open())
{
…
}
else
cout<<”File could not be
opened.”<<endl;
Any files opened by a program should be closed before
the program ends.
outFile.close();
inFile.close();
Program
Examples (in the class):
Example 1:
// To write records to a sequential access file
#include<iostream.h>
#include<fstream.h>
void main()
{
int sales = 0;
// create file object and to open it
ofstream outputf;
outputf.open("onlysales.dat", ios::out);
// Determine if the file was opened successfully
if(outputf.is_open())
{
cout<<"Enter the sale amount: $";
cin>>sales;
while(sales != -1)
{
// write record to file
outputf<<sales<<endl;
cout<<"Enter the sale amonut: $";
cin>>sales;
}// end of while
// close file
outputf.close();
}
else
cout<<"File could not be opened."<<endl;
}// end of main
Example 2:
// To read records from a sequential file which we made with the first example
#include<iostream.h>
#include<fstream.h>
void main()
{
int sales = 0;
// create file object and to open it for reading
ifstream inputf;
inputf.open("onlysales.dat", ios::in);
// Determine if the file was opened successfully
if(inputf.is_open())
{
cout<<"The list of sales:"<<endl;
while(!inputf.eof())
{
inputf>>sales; // we read sales from inputf
// write record to the screen
cout<<sales<<endl;
}// end of while
// close file
inputf.close();
}
else
cout<<"File could not be opened."<<endl;
}// end of main
Good luck with rest of the program
Esmaail M Nikjeh