Coders Packet

Maintaining Records using File handling in C++

By Sanskriti Saraswat

This C++ code helps to maintain student records in schools/colleges using the concept of fie handling with various other functions of C++ .

Hello everyone,

This C++ code helps to maintain student records in schools and colleges using the concept of file handling and various other C++ functions.

This code contains the following features:

  • Add student record
  • Update student record
  • Display student record
  • Add fields
  • Display fields details
  • Update Fields

The first function is to add the student records to the file. The second function is to update any records if needed.

The third function is to display all the records in the file. The fourth function is to add different fields to the file.

The fifth function is to display these field details. The sixth function is to update the fields if required.

A simple implementation of the switch case and the function call is used along with file handling and the concept of classes. Objects are created using the class name.

Using the switch case, any option can be selected and the respective operation can be performed any number of times until the user gives '0' as input which says "exit" and a final message is displayed at the end.

The code is :

#include 
#include 
#include 
#include
#include

using namespace std;

char stdRec[] = "Record.txt";
char fields[] = "fieldRecord.txt";
char tempFile[] = "tempFile.txt";

class Student{

public:

//add one students record 
int addRecord(char *stdRec){
int StdNumber;
char name[50];

ofstream out(stdRec, ios_base::app);

cout<<"\nEnter Student No : ";
cin>>StdNumber;
cout<<endl<<StdNumber<<" | ";

cout<<"\nEnter Name :";
cin>>name;
out<<name<<" | ";

int option=0;
cout<<"\n 1 - Under Graduate 2 - Post Graduate";
cout<<"\nEnter option :";
cin>>option;

int Count =0;

if(option==1)
Count = 6;
else if(option ==2)
Count = 4;
else{
cout<<"\n ERROR. Please enter 1 or 2 only ";
return 0;
}

for(int i=0; i<Count; i++)
{
int Field;
cout<<"\n Enter Field  Number"<<i<<" : ";
cin>>Field;
cout<<Field<<" | ";
}

char choice;
cout<<"\n Add Marks against each Field? Y/N :";
cin>>choice;

//separator to find start of marks
cout<<" = "; 

if(choice=='Y')
{
for(int i=0; i<Count; i++){
double marks;
cout<<"\n Enter Marks of Field Number"<<i<<" : ";
cin>>marks;
cout<<marks<<" | ";
}
  
}
}

//update any std's recrod 
int updateRecord(){
int StuNo;
cout<<"\nEnter Student Number :";
cin>>StuNo;
//modify if it exist e
}

//display record of any Student by his Student Number
int displayRecord(){
int StdNo;

//display if Std record exist
cout<<"\n Find Any Field Details #\n";

cout<<"\n Enter Student No to be searched: ";
cin>>StdNo;

int flag = -1;
ifstream file(stdRec);
string str;


while (getline(file, str))
{
if(str.length()>0){
  
char temp[1000];
memset(temp,1000,sizeof(temp));
strcpy(temp, str.c_str()); //copies the string

char * ptr;   //pointer
ptr = strtok(temp ,"|");

int temp_num = atoi(ptr);

if(StdNo == temp_num )
{
cout<<"\n"<<str;
flag = 1;
break;
}

}

}

if(flag==-1)
cout<<"\n No Studnt found with Number :"<<StdNo;

file.close();
getchar();getchar();
}

//display record of all Students
int displayAllRecord(){
ifstream file(stdRec);
string str; 
cout<<"\n List of All Students ";

if(!file)	{
cout<<"\n No Record found !!";
return -1;
}


while (getline(file, str))
{
if(str.length()>0)
cout<<"\n\t"<<str;
}
file.close();
getchar();getchar();
}

int findStudentMarks(){
cout<<"\n Find Marks of any Std \n";
int StdNo;

cout<<"\n Enter Student No : ";
cin>>StdNo;

int flag = -1;
ifstream file(stdRec);
string str;


while (getline(file, str))
{
if(str.length()>0){
  
char temp[1000];
memset(temp,1000, sizeof(temp));
strcpy(temp, str.c_str());

char * ptr;
ptr = strtok(temp ,"|");

int temp_num = atoi(ptr);

if(StdNo == temp_num )
{
memset(temp,1000,sizeof(temp));
strcpy(temp, str.c_str());
char *ptr1 = strtok(temp, "=");
ptr1 = strtok(NULL,"=");
//cout<<"\n====> "<<ptr1;
ptr1 = strtok(ptr1, "|");
double sum  = atof(ptr1);
double min_no = sum;
//cout<<"\nMin no is : "<< minNo;
int count = 0;
while(ptr1 = strtok(NULL,"|")){
//cout<<"-> "<<ptr1;
if(atof(ptr1)<min_no)
  min_no = atof(ptr1);

sum += atof(ptr1);
count++;
}
  
//cout<<"\nsum is : "<<sum;
//cout<<"\ncount sub : "<<countSub;
//cout<<"\nMIn no is : "<<min_no;

if(count == 6){
sum -= min_no;
cout<<"\n Sum is : "<<sum;
sum = sum / 5; 
cout<<"\n Avg Sum is top 5 sub: "<<sum;

if(sum>70) cout<<"\nGrade : A";
else if(sum>=60 && sum<=69)
cout<<"\nGrade is : B+";
else if(sum >=50 && sum<=59) 
cout<<"\nGrade is : B";
else if(sum>=41 && sum<=49) 
cout<<"\nGrade is : C";
else if(sum>=40) 
cout<<"\nGrade is : D";
else if(sum<40) 
cout<<"\nGrade is: F";

}
else{
sum = sum / 4; 
cout<<"\nAvg Sum of 4 sub : "<< sum;

if(sum>= 76) cout<<"\nDistinction";
else if(sum>=61 && sum<=75 ) 
cout<<"\nMerit";
else if(sum>=50 && sum<=60) 
cout<<"\nPass";
else if(sum<50) cout<<"\nFail ";    				
}

flag = 1;
break;
}

}

}

if(flag==-1)
cout<<"\n No Student found with No : "<<StdNo;

file.close();
getchar();getchar();	
}

int updateStd(){
  
cout<<"\n Update Std Record \n";
int StdNo;

cout<<"\n Enter Std No : ";
cin>>StdNo;

int flag = -1;
ifstream file(stdRec);
string str;
int StdFound = 0;
while (getline(file, str))
{
if(str.length()>0){
  
char temp[1000];
memset(temp,1000,sizeof(temp));
strcpy(temp, str.c_str());

char * ptr;
ptr = strtok(temp ,"|");

int tempno = atoi(ptr);

if(StdNo == tempno )
{
addRecord(tempFile);
StdFound = 1;
}
else{
ofstream file(tempFile, ios_base::app);
file<<endl<<str;
file.close();
  
}
}
}

file.close();

if(StdFound){
remove(stdRec);
rename(tempFile, stdRec);
}
  
}
};

class Field{

public:

int displayAnyFieldInfo(){
int fieldNo;	
cout<<"\n# Find Field Details #\n";

cout<<"\n Enter Field No :";
cin>>fieldNo;
int flag = -1;
ifstream file(fields);
string str;
while (getline(file, str))
{
if(str.length()>0){
char temp[1000];
memset(temp,1000,sizeof(temp));
strcpy(temp, str.c_str());

char * ptr;
ptr = strtok(temp ,"|");

int tempno = atoi(ptr);
cout<<"\n Field number is : "<<temp;

if(fieldNo == tempno )
{
cout<<"\n"<<str;
flag = 1;
break;
}

}

}

if(!flag)
cout<<"\n No Field found : "<<fieldNo;

file.close();
getchar();getchar();
  
  
}

int displayAllFieldInfo(){	
cout<<"\n List of All Fields \n";

//read all fields from record

ifstream file(fields);
string str; 
cout<<"\n Field No | Heading | Level";
while (getline(file, str))
{
if(str.length()>0)
cout<<"\n"<<str;
}
file.close();
getchar();getchar();
}

int updateFieldsInfo(){
cout<<"\n Update Field Info ";
int feildNo;
cout<<"\nEnter Fields No to be modified:";
cin>>feildNo;

}

//add new field to DB
int addField(char *fieldrecord){
int FieldNo;
char heading[50];
char level[50];

ofstream out(fieldrecord, ios_base::app);

cout<<"\n Add New field ";

cout<<"\n Enter field number : ";
cin>>FieldNo;
cout<<endl<<FieldNo<<" | ";

cout<<"\n Enter heading of feild :";
cin>>heading;
cout<<heading<<" | ";

cout<<"\n Enter Level UnderGrad/Graduate:";
cin>>level;
cout<<level<<" | ";   

cout<<"\n Field Added !!";
getchar();
}

int updateField(){
  
cout<<"\n Update Field Record \n";
int f_No;

cout<<"\n Enter Feild No : ";
cin>>f_No;

int flag = -1;
ifstream file(fields);
string str;


int field_found = 0;
while (getline(file, str))
{
if(str.length()>0){
  
char temp[1000];
memset(temp,1000,sizeof(temp));
strcpy(temp, str.c_str());

char * ptr;
ptr = strtok(temp ,"|");

int tempno = atoi(ptr);

if(f_No == tempno )
{
addField(tempFile);
field_found = 1;
}
else{
ofstream file(tempFile, ios_base::app);
file<<endl<<str;
file.close();		
}
}
}

file.close();

if(field_found){
remove(fields);
rename(tempFile, fields);
}
  	
}
};

int main(int argc, char** argv) {
Student stdObj;
Field fieldObject;
  
int choice = -1;

while(choice !=0){
system("cls");

cout<<"\n# College Registration System #";
cout<<"\n 1> Add Record";
cout<<"\n2> Update Record";
cout<<"\n3> Get any record";
cout<<"\n4> Get all record";
cout<<"\n5> Add Field";
cout<<"\n6> Get Any Field details";
cout<<"\n7> Get All Field details";
cout<<"\n8> Update Field";
cout<<"\n9> Find Std Marks";
cout<<"\n0> Exit.";

cout<<"\n Enter your choice : ";
cin>>choice;


switch(choice){
case 1:
stdObj.addRecord(stdRec); 
break;
case 2:
stdObj.updateStd(); 
break;
case 3:
stdObj.displayRecord(); 
break; 
case 4:
stdObj.displayAllRecord();
break;
case 5:
fieldObject.addField(fields); 
break;
case 6: 
fieldObject.displayAnyFieldInfo(); 
break;
case 7: 
fieldObject.displayAllFieldInfo();
break;
case 8: 
fieldObject.updateField(); 
break;
case 9:
stdObj.findStudentMarks(); 
break;
case 0: 
cout<<"\nThankyou !! Good Bye!!"<<endl; 
break;
default: 
cout<<"\nInvalid choice."; 
getchar(); 
}
}

return 0;
}

 

 

Download project

Reviews Report

Submitted by Sanskriti Saraswat (sanskriti24)

Download packets of source code on Coders Packet