Ok what i'm trying to do in this program is first create an empty binary file of size 31 records. Records includes studid & gpa. and then I insert valid record into the binary file with h(k) = key% 29..and then read this binary file sequentially and write the records and their position into output file.....
sample input file
31 2.34
62 1.55
60
3.25
333 3.33
38 3.90
-1 //read upto -1
studBinaryFile.h
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int MIN_ID = 11;
const int MAX_ID = 99;
const float MIN_GPA = 0.00;
const float MAX_GPA = 4.00;
class StudBinaryFile
{
public:
void setId(int idVal);
void setGpa(float gpaVal);
void ReadRd(int i,ifstream& inFile);
bool validRd();
void writeBinary(StudBinaryFile rd, ofstream& outFile);
void writeInvalidRd(ofstream& outFile2);
void readBinary(StudBinaryFile rd, ifstream& inFile, ofstream& outFile2);
void printData(int count, const StudBinaryFile& record, ofstream& outFile2);
private:
int id;
float gpa;
};
studBinaryFile.cxx
#include "studBinaryFile.h"
void StudBinaryFile::setId(int idVal)
{
id = idVal;
}
void StudBinaryFile::setGpa(float gpaVal)
{
gpa = gpaVal;
}
void StudBinaryFile::ReadRd(int i, ifstream& inFile)
{
id = i;
inFile >> gpa;
}
bool StudBinaryFile::validRd()
{
return ((id >= MIN_ID)&&
(id <= MAX_ID)&&
(gpa >= MIN_GPA)&&
(gpa <= MAX_GPA));
}
void StudBinaryFile::writeBinary(StudBinaryFile rd, ofstream& outFile)
{
outFile.seekp((id % 29) * sizeof(rd));
{outFile.write(reinterpret_cast<const char *> (&rd), sizeof(rd));}
}
void StudBinaryFile::readBinary(StudBinaryFile rd, ifstream& inFile, ofstream& outFile2)
{
int count = 0;
inFile.read(reinterpret_cast<char *> (&rd), sizeof(StudBinaryFile));
while(inFile)
{
if (id != 0)
{
printData(count, rd, outFile2);
count++;
}
inFile.read(reinterpret_cast<char *> (&rd), sizeof(StudBinaryFile));
}
}
void StudBinaryFile::writeInvalidRd(ofstream& outFile2)
{
outFile2 << id << setw(12) << gpa << endl;
}
void StudBinaryFile::printData(int count, const StudBinaryFile& record, ofstream& outFile2)
{
if(record.id != 0)
outFile2 << count << ".) " << record.id << setw(12) << record.gpa << endl;
else
outFile2 << count << ".) " << "Empty record" << endl;
count++;
}
run.cxx
#include "studBinaryFile.h"
int main()
{
ifstream inFile, inFile2;
ofstream outFile, outFile2;
inFile.open("in.data");
outFile.open("binrecords.bin",ios::out | ios::binary);
outFile2.open("out.data");
StudBinaryFile blankRd;
for (int i = 1; i<=31; i++)
{
blankRd.setId(0);
blankRd.setGpa(0.0);
outFile.write(reinterpret_cast<const char *> (&blankRd), sizeof(blankRd));
}
StudBinaryFile rd;
outFile.seekp(0L,ios::beg);
int spliter;
outFile2 << "*< Invalid Records Report >*" << endl;
inFile >> spliter;
while(spliter != -1)
{
rd.ReadRd(spliter,inFile);
if(rd.validRd())
{
rd.writeBinary(rd, outFile);
}
else
{rd.writeInvalidRd(outFile2);}
inFile >> spliter;
}
outFile2 << "<* end >*" << endl << endl;
inFile.close();
outFile.close();
outFile2 << "Reading random file sequentially" << endl;
inFile.open("binrecords.bin", ios::in | ios::binary);
rd.readBinary(rd, inFile, outFile2);
inFile.close();
return 0;
}
This is the output im getting
*< Invalid Records Report >*
333 3.33
<* end >*
Reading random file sequentially
0.) Empty record
1.) Empty record
2.) 60 3.25
3.) Empty record
4.) 62 1.55
5.) Empty record
6.) Empty record
7.) Empty record
8.) Empty record
9.) 38 3.9
10.) Empty record
11.) Empty record
12.) Empty record
13.) Empty record
14.) Empty record
15.) Empty record
16.) Empty record
17.) Empty record
18.) Empty record
19.) Empty record
20.) Empty record
21.) Empty record
22.) Empty record
23.) Empty record
24.) Empty record
25.) Empty record
26.) Empty record
27.) Empty record
28.) Empty record
29.) Empty record
30.) Empty record
This is the output i want
*< Invalid Records Report >*
333 3.33
<* end >*
Reading random file sequentially
0.) Empty record
1.) Empty record
2.) 31 2.34
3.) 60 3.25
4.) 62 1.55
5.) Empty record
6.) Empty record
7.) Empty record
8.) Empty record
9.) 38 3.9
10.) Empty record
11.) Empty record
12.) Empty record
13.) Empty record
14.) Empty record
15.) Empty record
16.) Empty record
17.) Empty record
18.) Empty record
19.) Empty record
20.) Empty record
21.) Empty record
22.) Empty record
23.) Empty record
24.) Empty record
25.) Empty record
26.) Empty record
27.) Empty record
28.) Empty record
29.) Empty record
30.) Empty record
This program works fine except that its overriding some of the record because of collision problem ..someone please help me here