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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
#include<fstream>
#include<string>
#include<iostream>
using namespace std;
struct Date
{
int day;
int month;
int year;
};
struct Person
{
char name[20];
double score;
Date testTaken;
};
int main()
{
Person *someone;
string fileT,
fileB;
int num = 0;
cout << "Enter the name of the file with the testers information:\n";
cin >> fileT;
cout << "Enter the name of the binary file:\n";
cin >> fileB;
cout << "I will now attempt to open and read the text file.\n";
someone = readTesters(fileT, num);
cout << "I will now attempt to to write the text file data into a binary file.\n";
writeTesters(fileT, fileB, someone, num);
return 0;
}
Person* readTesters(const string& fileTxt, int& num)
{
int numP;
fstream inFile;
inFile.open(fileTxt, ios::in);
if (inFile.fail())
{
cout << "Error opening testers file!\n";
}
else if (inFile)
{
string score,
name,
day,
month,
year;
cout << "Testers file opened. Now reading file...\n";
inFile >> numP;
Person *people = new Person[numP];
while (inFile)
{
for (int i = 0; i < numP; i++)
{
inFile.getline(people[i].name, 20, '\n'); //not reading the names correctly
getline(inFile, score, ',');
people[i].score = atof(score.c_str());
getline(inFile, day, '/');
people[i].testTaken.day = atoi(day.c_str());
getline(inFile, month, '/');
people[i].testTaken.month = atoi(month.c_str());
getline(inFile, year);
people[i].testTaken.year = atoi(year.c_str());
}
}
num = numP;
inFile.close();
return people;
}
}
void writeTesters(const string& fileTxt, const string& fileDat, Person people[], int numP)
{
fstream file;
char newline = '\n';
char slash = '/';
file.open(fileDat, ios::out | ios::binary);
if (file.fail())
{
cout << "Error opening binary file!\n";
}
else if (file)
{
cout << "File opened. Now writing data into file...\n"; //when i run the program, this is as far as it goes. it doesnt show anything else
for (int i = 0; i < numP; i++)
{
file.write(reinterpret_cast<char *>(numP), sizeof(numP));
file.write(reinterpret_cast<char *>(newline), sizeof(newline));
file.write(reinterpret_cast<char *>(people[i].name), sizeof(people[i].name));
file.write(reinterpret_cast<char *>(newline), sizeof(newline));
file.write(reinterpret_cast<char *>(&people[i].score), sizeof(people[i].score));
file.write(reinterpret_cast<char *>(newline), sizeof(newline));
file.write(reinterpret_cast<char *>(people[i].testTaken.day), sizeof(people[i].testTaken.day));
file.write(reinterpret_cast<char *>(slash), sizeof(slash));
file.write(reinterpret_cast<char *>(people[i].testTaken.month), sizeof(people[i].testTaken.month));
file.write(reinterpret_cast<char *>(slash), sizeof(slash));
file.write(reinterpret_cast<char *>(people[i].testTaken.month), sizeof(people[i].testTaken.month));
file.write(reinterpret_cast<char *>(newline), sizeof(newline));
}
cout << "Data written into file.\n\n";
}
cout << "Now closing the file...\n";
file.close();
cout << "Opening the file again for input in binary mode...\n";
file.open(fileDat, ios::in | ios::binary);
cout << "Now reading the data back into memory...\n";
for (int i = 0; i < numP; i++)
{
file.read(reinterpret_cast<char *>(numP), sizeof(numP));
file.read(reinterpret_cast<char *>(newline), sizeof(newline));
file.read(reinterpret_cast<char *>(people[i].name), sizeof(people[i].name));
file.read(reinterpret_cast<char *>(newline), sizeof(newline));
file.read(reinterpret_cast<char *>(&people[i].score), sizeof(people[i].score));
file.read(reinterpret_cast<char *>(newline), sizeof(newline));
file.read(reinterpret_cast<char *>(people[i].testTaken.day), sizeof(people[i].testTaken.day));
file.read(reinterpret_cast<char *>(slash), sizeof(slash));
file.read(reinterpret_cast<char *>(people[i].testTaken.month), sizeof(people[i].testTaken.month));
file.read(reinterpret_cast<char *>(slash), sizeof(slash));
file.read(reinterpret_cast<char *>(people[i].testTaken.month), sizeof(people[i].testTaken.month));
file.read(reinterpret_cast<char *>(newline), sizeof(newline));
}
cout << "Here is what I stored in the binary file:\n";
for (int i = 0; i < numP; i++)
{
cout << "Tester #" << (i + 1) << endl;
cout << "Name: " << people[i].name << endl;
cout << "Score: " << people[i].score << endl;
cout << "Test Date: " << people[i].testTaken.day << "/" << people[i].testTaken.month
<< "/" << people[i].testTaken.year << endl << endl;
}
file.close();
}
| |