Okkk, here goes. This is my code so far, and it appears my binary file either isn't working right, or just isn't outputing to my outFile (i'll go for probably the first option. Here is my code:
Code:
Binary.h
#include<iostream>
#include<fstream>
using namespace std;
class BinaryFile
{
public:
void readFromFile(ifstream& inFile);
void validRead(BinaryFile rd);
void writeBinary(BinaryFile rd, ofstream& outFile);
void readBinary(BinaryFile rd, ifstream& inFile, ofstream& outFile);
void outputRd(ofstream& outFile, BinaryFile rd);
int acctNum;
float acctBalance;
};
Binary.cxx
#include "Binary.h"
void BinaryFile::readFromFile(ifstream& inFile)
{
int holder;
holder = inFile.peek();
// if (holder = -1)
// continue;
// else
inFile >> acctNum >> acctBalance;
}
void BinaryFile::writeBinary(BinaryFile rd, ofstream& outFile)
{
outFile.seekp((rd.acctNum % 97) * sizeof(rd));
outFile.write((char*)&rd, sizeof(rd));
}
void BinaryFile::readBinary(BinaryFile rd, ifstream& inFile, ofstream& outFile)
{
inFile.read((char*)&rd, sizeof(BinaryFile));
while(!inFile.eof())
{
if (acctNum != 0)
{
outFile << acctNum << acctBalance;
}
inFile.read((char*)&rd, sizeof(BinaryFile));
}
}
void BinaryFile::outputRd(ofstream& outFile, BinaryFile rd)
{
outFile << acctNum << acctBalance;
}
runBinary.cxx
#include "Binary.h"
int main()
{
ifstream inFile;
ofstream outFile;
inFile.open("in.txt");
outFile.open("binrecords.bin", ios::out | ios::binary);
int blank;
istream& read(char array[], int numberOfChar);
ostream& write(const char array[], int numberOfChar);
BinaryFile blankRd;
for (int i = 1; i <= 100; i++)
{
outFile.write((char*)&blankRd, sizeof(blankRd));
cout.write((char*)&blankRd, sizeof(blankRd));
}
BinaryFile rd;
while(!inFile.eof())
{
rd.readFromFile(inFile);
cout << rd.acctNum << rd.acctBalance << endl;
rd.writeBinary(rd,outFile);
}
inFile.close();
outFile.close();
inFile.open("binrecords.bin", ios::in);
outFile.open("out.txt");
rd.readBinary(rd, inFile, outFile);
return 0;
}
Ok, now I know that it is reading correctly from an inFile, but that's about it. I don't know if it's inputting correctly into the binary file (or if the binary file is created correctly), or inputting correctly into the outFie. What the program has to do is take numbers form an inFile (acctNum and acctBalance) and put them into a binary file that is 100 records long. I then have to output them sequentially and randomly, and put in an "empty" when there is nothing there. Example of the output:
0) 22 $22.36
1) 33 $333.33
2) Empty
3) Empty
4) 58 $5.23
etc.....