This is a homework assignment, and I'm not looking for anyone to do my homework, but I have looked everywhere on the internet, but I guess I just don't know where to look.
I have a file 'quizzes.dat' which, when opened in notepad looks like this:
"Bart Simpson K A F Ralph Wiggum # < , Lisa Simpson d b [ Martin Prince c b c Milhouse Van Houten P W O "
all on one line.
My task is to take this binary file and output a readable text file using fstream and the read/ write functions.
The code I have so far is fairly straight forward:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool openInFile (ifstream &, char []);
bool openOutFile (ofstream &, char []);
struct student
{
char name[25];
int exam1;
int exam2;
int exam3;
};
student bclass[5];
int main()
{
ifstream input;
openInFile (input, "quizzes.dat");
ofstream output;
openOutFile (output, "quizzes.txt");
output << "=================================================\n";
while (!input.eof())
{
for (int i = 0; i < 5; i++)
{
input.read((char*)&bclass[i], sizeof(bclass[i]));
output << bclass[i].name << ", " << bclass[i].exam1 << ", "
<< bclass[i].exam2 << ", " << bclass[i].exam3 << endl;
}
}
output << "=================================================\n";
input.close();
output.close();
return 0;
}
// Opens and checks input file
bool openInFile (ifstream &in, char filename[])
{
in.open(filename, ios::in | ios::binary);
if (in.fail())
{
cout << "ERROR: Cannot open quizzes.dat\n";
exit(0);
}
return in.fail();
}
// Opens and checks output file
bool openOutFile (ofstream &out, char filename[])
{
out.open(filename, ios::out);
if (out.fail())
{
cout << "ERROR: Cannot open quizzes.txt\n";
exit(0);
}
return out.fail();
}
|
Firstly, is this the best way to read the binary file? Or is it not a good idea to make an array of the struct. On our assignment we are told the binary file follows the pattern of the struct, one name of 25 characters, and 3 int quiz grades, with a total of 5 students.
Lastly the output I get in my text file is:
=================================================
Bart Simpson, 16640, 17920, 1818317312
ph Wiggum, 2883584, 1766588416, 1394631027
impson, 1291845632, 1769239137, 1917853806
ince, 1751935309, 1702065519, 1851872800
Houten, 0, 0, 0
=================================================
when it should look something like:
=================================================
Bart Simpson, 75, 65, 70
Ralpph Wiggum, 35, 60, 44
Lisa Simpson, 100, 98, 91
Martin Prince, 99, 98, 99
Milhouse Van Houten, 80, 87, 79
=================================================
When analyzing the dat file in notepad, I see that each name has 25 spaces allocated to it, and that the unreadable parts each have 4 spaces which I assume correlate to 4 bytes of an integer type.
My question is how do I convert that data to a readable text format, and why are the names getting cut off if the data does seem to follow the exact pattern as my struct? Please help!