Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

So I need help with binary files. I\'m using Microsoft Studio 2017. This is what

ID: 3850980 • Letter: S

Question

So I need help with binary files. I'm using Microsoft Studio 2017.

This is what i need help with: Only change is: you will first write student structures that were given in Homework Assignment 2 into a binary file and read those student structures from binary file in to your students array. Just make necessary changes in your code in Assignment 2 to work with binary files.

This is my previous code.

#include<iostream>

#include<string>

#include<fstream>

#include<iomanip>

#include<stdlib.h>

using namespace std;

void readDataFromFile();

void calculateResult();

void displayResult();

void removeCharsFromString(string &str);

char file_name[20] = "students.txt";

int total_record = 0;

char answer_keys[5];

struct Student

{

   int stud_id;

   string full_name, answers;

   char lLtterGde;

   double total_points, avg;

   int operator=(const Student& s)

   {      

       this->stud_id = s.stud_id;

       this->full_name = s.full_name;

       this->lLtterGde = s.lLtterGde;

       this->answers = s.answers;

       this->total_points = s.total_points;

       this->avg = s.avg;

       return 0;

   }

}record[50];

void StudentSort()

{

   int li, lj;

   Student check;

   for (li = 0; li < total_record; li++) {

       check = record[li];

       for (lj = li; lj >= 1 && (check.full_name < record[lj - 1].full_name); lj--)

       {

           record[lj] = record[lj - 1];

           record[lj - 1] = check;

       }

   }

}

int main()

{

   cout << endl << "Enter Answer Keys : ";

   for (int li = 0; li<5; li++)

   {

       cout << endl << "Answer " << li + 1 << " : ? ";

       cin >> answer_keys[li];

   }

   calculateResult();

   displayResult();

   system("pause");

   return 0;

}

void readDataFromFile()

{

   ifstream ifile;

   ifile.open(file_name);

   total_record = 0;

   string line = "";

   if (ifile)

   {

       while (!ifile.eof())

       {

           getline(ifile, line);

           string id = line.substr(0, line.find(" "));

           record[total_record].stud_id = atoi(id.c_str());

           record[total_record].full_name = line.substr(line.find(" ") + 1);

           line = "";

           getline(ifile, line);

           record[total_record].answers = line;

           total_record++;

       }

       if (total_record > 0)

           total_record--;

   }

   else

       cout << "Error";

   ifile.close();

   StudentSort();

}

void calculateResult()

{

   cout << endl << "Grade Report" << endl;

   cout << endl << "Student ID Student Name Answers Total Pts Average Letter Grade ";

   readDataFromFile();

   for (int li = 0; li < total_record; li++)

   {

       removeCharsFromString(record[li].answers);

       int points = 0;

       for (int lj = 0; lj<10; lj++)

       {

           if (record[li].answers[lj] == answer_keys[lj])

           {

           points += 5;

           }

       }

       record[li].total_points = points;

       record[li].avg = (points * 100 / 50);

       if (record[li].avg >= 90)

       {

       record[li].lLtterGde = 'A';

       }

       else if (record[li].avg >= 80 && record[li].avg <= 89)

       {

           record[li].lLtterGde = 'B';

       }

       else if (record[li].avg >= 70 && record[li].avg <= 79)

       {

           record[li].lLtterGde = 'C';

       }

       else if (record[li].avg >= 60 && record[li].avg <= 69)

       {

           record[li].lLtterGde = 'D';

       }

       else

       {

           record[li].lLtterGde = 'F';

       }

       cout << endl << setw(4) << record[li].stud_id << setw(22) << record[li].full_name << setw(12) << record[li].answers << setw(8) << record[li].total_points << setw(8) << record[li].avg << setw(8) << record[li].lLtterGde;

   }

}

void displayResult()

{

   cout << endl << "Student Admitted to the Graduate Program" << endl;

   cout << endl << "Student ID Student Name Total Pts Average Letter Grade ";

   for (int li = 0; li < total_record; li++)

   {

       if (record[li].lLtterGde == 'A' || record[li].lLtterGde == 'B' || record[li].lLtterGde == 'C')

           cout << endl << setw(4) << record[li].stud_id << setw(22) << record[li].full_name << setw(8) << record[li].total_points << setw(8) << record[li].avg << setw(8) << record[li].lLtterGde;

   }

   cout << endl << "Student with Conditional Admission to the Graduate Program" << endl;

   cout << endl << "Student ID Student Name Total Pts Average Letter Grade ";

   for (int li = 0; li < total_record; li++)

   {

       if (record[li].lLtterGde == 'D')

           cout << endl << setw(4) << record[li].stud_id << setw(22) << record[li].full_name << setw(8) << record[li].total_points << setw(8) << record[li].avg << setw(8) << record[li].lLtterGde;

   }

   cout << endl << "Student not Allowed Admission" << endl;

   cout << endl << "Student ID Student Name Total Pts Average Letter Grade ";

   for (int li = 0; li < total_record; li++)

   {

       if (record[li].lLtterGde == 'F')

       cout << endl << setw(4) << record[li].stud_id << setw(22) << record[li].full_name << setw(8) << record[li].total_points << setw(8) << record[li].avg << setw(8) << record[li].lLtterGde;

   }

}

void removeCharsFromString(string &str)

{

   //str.erase(remove(str.begin(), str.end(), ' '), str.end());

}

Explanation / Answer

You didn't give the original question or whatever the program is supposed to do. Neither did you give the txt file. So i have nothing to work with.
But i can teach you how to read and write structs into binary files.
let us consider a program:
struct something
{

int a,b;

char c[20];
}a[10];

if this is the struct, and you need to write it to a binary file, first open a file using ofstream.

ofstream fout("student.dat",ios::binary|ios::out);

then,

using the write function, you can write structs into the file.

We will use a loop in our example:
for(int i=0;i<10;i++)

fout.write((char*)&a[i],sizeof(something));

This loop will write all the structs to the binary file.

Now, to read them, open a file for reading using ifstream

ifstream fin.open("student.dat",ios::in|ios::binary);

use the same loop but instead of write, use read now

for(int i=0;i<10;i++)

fin.read((char*)&a[i],sizeof(something));

Hope this helps.
:)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote