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.
I want 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:
#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
? I was 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!
https://gist.github.com/anonymous/5237202 This is how I would do it, I commented code, hope it help you in some way. 1. Nothing wrong with reading binary in that way 2. It's perfectly fine to make array of struct, like with any other variable.
Yes, binary follows patter of struct, name examOnePoints examTwoPoints examThreePoints, but since names have different lengths you can't just throw all values from binary file into memory location from where struct begins and onward.
Also, I used array of 3 integers to store exam points instead of 3 separate variables since it was easier to code, it can be done either way. One more thing, I would suggest you to download some free hexeditor and examine the .dat file a bit, it will help you understand why I read points that way.