-
Binary file problems..
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.....
-
I know it's commented out, but still:
Code:
// if (holder = -1)
In your read function, print out each line that's read in, so you can see if it's what you want.
What's in the output file?
-
Nothing shows up in my outFile. I know that it is inputting fine (not positive if inputting into the binfile right though) because I did a cout before it inputted into the binfile. I think it might be a problem retrieving the data from the inFile.
-
Okay, so print each line as you read it in. Then you can tell if you read in the data or not.
-
I did, right before it's inputted into the binary file it reads ok. I can't read line from line in my binary file because, for one, i can't just manually read it, two, i don't know if my code is working right to read it out of the binary file. I actually meant i think my problem is retrieving data from my binfile.