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 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;
const int MAX = 100;
struct LocalStudent
{
char name[MAX];
float mark[MAX];
};
struct ForeignStudent
{
char country [MAX];
char name[MAX];
float mark[MAX];
};
union AllStudent
{
LocalStudent ls;
ForeignStudent fs;
};
struct Student
{
char type;
int noAssessment;
int final;
char grade [MAX];
AllStudent st;
};
void createBinary (ifstream&, char *, fstream&, char *);
void processText (ifstream&, char *, fstream&, char *);
.
.
.
.
.
.
void createBinary (ifstream& infile, char *in_textFileName, fstream& afile, char *out_binFileName)
{
infile.open (in_textFileName, ios::in);
afile.open (out_binFileName, ios::out | ios::binary);
if(!infile.good())
{
cout << in_textFileName << " opened for reading failed" << endl
<< "End of task"<< endl;
return;
}
cout << in_textFileName << " sucessfully opened for reading" << endl;
if(!afile.good())
{
cout << out_binFileName << " opened for writing failed" << endl
<< "End of task"<< endl;
return;
}
cout << out_binFileName << " successfully opened for writing" << endl;
char n;
Student stud;
int no;
int a = 0;
while (infile >> n)
{
char ch;
infile.get(ch);
switch (n)
{
case 'F': stud.type = n;
infile >> stud.st.fs.country;
infile.getline(stud.st.fs.name, MAX);
break;
case 'S': stud.type = n;
infile.getline(stud.st.ls.name, MAX);
break;
}
afile.write (reinterpret_cast <const char *> (&stud), sizeof (stud));
}
cout << "How many assessment components? ";
cin >> no;
stud.noAssessment = no;
for(int a = 0; a < 0; ++a)
{
cin >> stud.st.ls.mark[a];
}
infile.close();
cout << in_textFileName << " successfully closed" <<endl;
afile.close();
cout << out_binFileName << " successfully created" << endl;
}
//Process of assessment template (text file)
void processText(ifstream& infile, char *in_binFileName, fstream& afile, char *out_textFileName)
{
infile.open (in_binFileName, ios::binary | ios::in);
afile.open (out_textFileName, ios::out);
if (!infile.good())
{
cout << in_binFileName << " opened for reading failed " << endl;
return;
}
cout << in_binFileName << " sucessfully opened for reading" << endl;
if(!afile.good())
{
cout << out_textFileName << " opened for writing failed" << endl;
return;
}
cout << out_textFileName << " successfully opened for writing" << endl;
Student stud;
int i = 0;
while (infile.read (reinterpret_cast <char *>(&stud), sizeof (stud)))
{
afile << ++i << "\t";
if (stud.type == 'F')
afile << stud.st.fs.country //<< "\t"
<< stud.st.fs.name << "\t"
// << stud.noAssessment << "\t"
<< stud.final << "\t"
// << stud.st.fs.grade << "\t"
<< endl;
else if (stud.type == 'S')
afile << "Singapore" << "\t"
<< stud.st.ls.name << "\t"
<< stud.st.ls.mark << "\t"
<< stud.final << "\t"
// << stud.st.ls.grade << "\t"
<< endl;
}
infile.close ();
cout << in_binFileName << " successfully closed" << endl;
afile.close ();
cout << out_textFileName << " successfully created" << endl;
}
Actual output wanted:
|