I'm having difficulties to get the buffer read 10 numbers from a file, then after it's empty read next 10 numbers, and so on till eof is reached. The input file contains several lines of data, with a single integer value on each line. Here is my code (I have commented where help is needed):
#include <fstream>
#include <iostream>
using namespace std;
const int MAXLEN = 10;
class BufferClass
{
private:
int buffer[MAXLEN];
ifstream f;
public:
BufferClass();
~BufferClass();
void startNumber();
void getNumber(int &num, bool &endfile);
void printNumber(int &num);
};
BufferClass::BufferClass()
{
//no use, since startNumber() takes care of opening the file?!?
}
BufferClass::~BufferClass()
{
f.close();
}
//sets whatever is necessary to cause getNumber to return
// values beginning at the first number in the file
void BufferClass::startNumber()
{
f.open("p2355.dat");
}
//getNumber() returns the value of the next number stored in a disk file
//and set endfile to false, or set endfile to true if there
//is no more data in the file
//getNumber() has to interface with the disk file through
//a 10 element buffer (i.e. at most 10 numbers from
//the fileare resident in memory at one time
void BufferClass::getNumber(int &num, bool &endfile)
{
//read ten numbers from file
//
}
void BufferClass::printNumber(int &num)
{
cout << num << endl;
}
int main(void)
{
BufferClass buffer;
int num;
bool endfile;
buffer.startNumber();
buffer.getNumber(num, endfile);
while(!endfile)
{
buffer.printNumber(num);
buffer.getNumber(num, endfile);
}
return 0;
}
Thank you,
Waldis