-
-
Save dahabit/5449673 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <windows.h> | |
using namespace std; | |
bool openInFile (ifstream &, char []); | |
bool openOutFile (ofstream &, char []); | |
struct student | |
{ | |
char name[25]; | |
int exam[3]; | |
}; | |
student bclass; | |
int main() | |
{ | |
char bfr; //Since we are reading from binary file, we are reading byte by byte, byte goes from 0x00 to 0xFF hence our buffer that we will use must be right type | |
// !!!! You can't use integer, cause int's have 4 bytes, and when u write one byte to it (remember we r reading bytes, and points from every exam are represented by 1 byte) u will only fill first byte | |
//And cause of that rest 3 bytes will have some random values that were assigned to them when our variable was declared !!!! | |
int firstNameLength = 0; | |
int lastNameLength = 0; | |
ifstream input; | |
openInFile (input, "quizzes.dat"); | |
ofstream output; | |
openOutFile (output, "quizzes.txt"); | |
output << "=================================================\n"; | |
for(int i = 0; i < 5; i++) | |
{ | |
input.ignore(25, 0x20); //Ignoring first 25 chars or ignoring x chars until we bump onto ' ' (if u open .dat file with some hexeditor u will see that space is representet by 0x20 hexadecimal) | |
firstNameLength = input.gcount(); //firstNameLength = number of chars that we passed until we found ' ' | |
input.ignore(25, 0x20); //Ignoring next 25 chars or ignoring x chars until we bump onto ' ' | |
lastNameLength = firstNameLength + input.gcount(); //lastNameLength = firstNameLength + number of chars we passed until we found another ' ', lastNameLength now holds value that is length of our name and lastname together | |
if(i == 4) //Milhouse have middle name included so we must do this once again in his case so we don't skip his lastname | |
{ | |
input.ignore(24, ' '); | |
lastNameLength += input.gcount(); | |
} | |
for(int i = 0; i < lastNameLength; i++) | |
{ | |
input.unget(); //Decrease the current location in the stream by one character, and repeting so until we get to the first char of the first name | |
} | |
input.readsome((char*)&bclass.name, lastNameLength); //Since we moved current location in the stream to the first char of the first name we are taking lastNameLength chars and putting them in bclass.name | |
for(int i = 0; i < 3; i++) //Until we find all 3 right values, we have 3 exams right? | |
{ | |
input.read((char*)&bfr, 1); //Read one char from stream | |
if(bfr != 0x20) //If char that we read is not ' ' (0x20 hex) than it's some value that probably represents number of points of particular exam, thats what we are looking for | |
{ | |
bclass.exam[i] = (int)bfr; //We take that char, cast it to int since our bclass.exam is type int, and store it in corresponding exam | |
input.get(); //Than we increase the curret location in the stream by one | |
}else //else, if char that we found is space, we just skip it and reapeat | |
{ | |
input.get(); | |
} | |
} | |
output << bclass.name << " " << bclass.exam[0] << " " << bclass.exam[1] << " " << bclass.exam[2] << endl; | |
} | |
output << "=================================================\n"; | |
input.close(); | |
output.close(); | |
system("pause"); | |
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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment