home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C Programming Starter Kit 2.0
/
SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso
/
tybc4
/
ioerr1.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1994-01-26
|
5KB
|
213 lines
/*
C++ program that demonstrates sequential binary file I/O
*/
#include <iostream.h>
#include <fstream.h>
const unsigned MIN_SIZE = 10;
const double BAD_VALUE = -1.0e+30;
enum boolean { false, true };
class TErrIO
{};
class TErrIndex
{};
class Array
{
protected:
double *dataPtr;
unsigned size;
double badIndex;
TErrIO ErrIO;
TErrIndex ErrIndex;
public:
Array(unsigned Size = MIN_SIZE);
~Array()
{ delete [] dataPtr; }
unsigned getSize() const { return size; }
double& operator [](unsigned index)
{ return (index < size) ? *(dataPtr + index) : badIndex; }
void writeElem(fstream& os, unsigned index)
throw(TErrIO, TErrIndex);
void readElem(fstream& is, unsigned index)
throw(TErrIO, TErrIndex);
void writeArray(const char* filename)
throw(TErrIO);
void readArray(const char* filename)
throw(TErrIO);
};
Array::Array(unsigned Size)
{
size = (Size < MIN_SIZE) ? MIN_SIZE : Size;
badIndex = BAD_VALUE;
dataPtr = new double[size];
}
void Array::writeElem(fstream& os, unsigned index)
throw(TErrIO, TErrIndex)
{
if (index < size) {
os.write((unsigned char*)(dataPtr + index), sizeof(double));
if(!os.good())
throw(ErrIO);
}
else
throw(ErrIndex);
}
void Array::readElem(fstream& is, unsigned index)
throw(TErrIO, TErrIndex)
{
if (index < size) {
is.read((unsigned char*)(dataPtr + index), sizeof(double));
if (!is.good())
throw(ErrIO);
}
else
throw(ErrIndex);
}
void Array::writeArray(const char* filename)
throw(TErrIO)
{
fstream f(filename, ios::out | ios::binary);
if (f.fail())
throw(ErrIO);
f.write((unsigned char*) &size, sizeof(size));
f.write((unsigned char*)dataPtr, size * sizeof(double));
f.close();
if (!f.good())
throw(ErrIO);
}
void Array::readArray(const char* filename)
throw(TErrIO)
{
fstream f(filename, ios::in | ios::binary);
unsigned sz;
if (f.fail())
throw(ErrIO);
f.read((unsigned char*) &sz, sizeof(sz));
// need to expand the array
if (sz != size) {
delete [] dataPtr;
dataPtr = new double[sz];
size = sz;
}
f.read((unsigned char*)dataPtr, size * sizeof(double));
f.close();
if (!f.good())
throw(ErrIO);
}
main()
{
const unsigned SIZE1 = 10;
const unsigned SIZE2 = 20;
char* filename1 = "array1.dat";
char* filename2 = "array3.dat";
int hiIndex = 10;
Array arr1(SIZE1), arr2(SIZE1), arr3(SIZE2);
fstream f(filename1, ios::out | ios::binary);
// assign values to array arr1
for (unsigned i = 0; i < arr1.getSize(); i++)
arr1[i] = 10 * i;
// assign values to array arr3
for (i = 0; i < SIZE2; i++)
arr3[i] = i;
cout << "Array arr1 has the following values:\n";
for (i = 0; i < arr1.getSize(); i++)
cout << arr1[i] << " ";
cout << "\n\n";
try {
// write elements of array arr1 to the stream
for (i = 0; i < arr1.getSize(); i++)
arr1.writeElem(f, i);
}
catch(TErrIO e) {
cout << "Bad stream output\n";
}
catch(TErrIndex e) {
cout << "Error in writing element " << i << "\n";
}
f.close();
// reopen the stream for input
f.open(filename1, ios::in | ios::binary);
try {
for (i = 0; i < arr1.getSize(); i++)
arr2.readElem(f, i);
}
catch(TErrIO e) {
cout << "Bad stream output\n";
}
catch(TErrIndex e) {
cout << "Error in writing element " << i << "\n";
}
f.close();
// display the elements of array arr2
cout << "Array arr2 has the following values:\n";
for (i = 0; i < arr2.getSize(); i++)
cout << arr2[i] << " ";
cout << "\n\n";
// display the elements of array arr3
cout << "Array arr3 has the following values:\n";
for (i = 0; i < arr3.getSize(); i++)
cout << arr3[i] << " ";
cout << "\n\n";
// write the array arr3 to file ARRAY3.DAT
try {
arr3.writeArray(filename2);
}
catch(TErrIO e) {
cout << "Cannot write the entire array\n";
}
try {
// read the array arr1 from file ARRAY3.DAT
arr1.readArray(filename2);
}
catch(TErrIO e) {
cout << "Cannot read the entire array\n";
}
// display the elements of array arr1
cout << "Array arr1 now has the following values:\n";
for (i = 0; i < arr1.getSize(); i++)
cout << arr1[i] << " ";
cout << "\n\n";
// reopen the stream for input
f.open(filename1, ios::in | ios::binary);
for (i = 0; i < 3; i++) {
hiIndex *= 10;
// attempt to read an element at index hiIndex
try {
arr1.readElem(f, hiIndex);
cout << "Element at index " << hiIndex << " = "
<< arr1[hiIndex] << "\n";
}
catch(TErrIndex) {
cout << "Failed to read element at index "
<< hiIndex << "\n";
}
}
f.close();
return 0;
}