How to add and retrieve record in a binary file using C++
Posted by Samath
Last Updated: January 06, 2021

Write a program that add and retrieve individual record to a binary file using the C++ programming language. In this program, the user is going to add 10 questions with there possible choice of (a, b, c, or d) and the correct answer for the question to the binary file. whenever a student is doing the quiz the program will randomly choose a question from the file and present it to the student.

#include <fstream>
#include <iostream>
#include <vector>
#include <string.h>
#include <stdio.h>
#include <ctime>
#include <cstdlib>
 
using namespace std;
 
 
typedef struct English
{
    char question[50];
    char a[50];
    char b[50];
    char c[50];
    char d[50];
    char answer;
    
}english_1;
 
int main(int argc, char *argv[])
{
	int option;
	cout<<"\t\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"<<endl;
	cout<<"\t\tPress 1 to add questions"<<endl;
	cout<<"\t\tPress 2 to take quiz"<<endl;
	cout<<"\t\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"<<endl;
	cout<<"\t\tOption: ";
	cin>>option;
	
	if(option == 1)
	{
		english_1 eng[10];
		for(int i = 0; i < 10; i++)
		{
			
			cout<<endl<<"Enter question #"<<i+1<<": " ;
			fflush(stdin);
	        cin.getline(eng[i].question, 50);
		
		cout<<"Enter option a: ";
		cin.getline(eng[i].a, 50);
		
		cout<<"Enter option b: ";
        cin.getline(eng[i].b, 50);
		
		cout<<"Enter option c: ";
	    cin.getline(eng[i].c, 50);
		
		cout<<"Enter option d: ";
		cin.getline(eng[i].d, 50);
		
		cout<<"Enter the correct answer(a, b, c or d): ";
		cin>>eng[i].answer;
		
	
		}  
		
        ofstream output_file("quiz.data", ios::binary | ios::app);
        output_file.write((char*)&eng, sizeof(eng));
        output_file.close();
	
	}
	else if(option == 2)
	{ 
	    vector<english_1> quest;
		char answer_for_question;
		ifstream input_file("quiz.data", ios::binary);
        english_1 master[10];
       input_file.read((char*)&master, sizeof(master));  
	   int random_integer[10]; 
	   int correct_answer = 0;
	   int wrong_answer = 0;
	   int i;
 
    for (size_t idx = 0; idx < 10; idx++)
    {
      quest.push_back(master[idx]);
    }
 
    for(i = 0; i < quest.size(); i++)
    {	
    
      srand((unsigned)time(0));
      random_integer[i] = rand() % quest.size();
    
    
    
      cout << quest[random_integer[i]].question<< endl;
      cout << "a: "<<quest[random_integer[i]].a<< endl;
        cout << "b: "<<quest[random_integer[i]].b<< endl;
        cout << "c: "<<quest[random_integer[i]].c<< endl;
        cout << "d: "<<quest[random_integer[i]].d<< endl;
        cout << "Answer: ";
        cin >> answer_for_question;
        
        if(answer_for_question == quest[random_integer[i]].answer)
        {
        	cout<<endl;
        	cout<<"Correct!"<<endl;
        	correct_answer++;
        	system("pause");
        	system("cls");
        }
        else
        {
		    cout<<endl;
        	cout<<"Wrong Answer"<<endl;
        	wrong_answer++;
        	system("pause");
        	system("cls");
        }
    
    }
cout<<endl<<"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"<<endl;
cout<<"Incorrect: "<<wrong_answer<<endl;
cout<<"Correct: "<<correct_answer<<endl;
cout<<"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"<<endl;
 		
	}
	return 0;
}