0% found this document useful (0 votes)
37 views3 pages

4

This C++ program reads student records from a text file into an array, allows the user to search for a record by registration number or add a new record. It includes function definitions to get the number of records, search by registration number, and write a new record to the file. The main function uses a switch menu to call the writeRecord and search functions, or exit the program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views3 pages

4

This C++ program reads student records from a text file into an array, allows the user to search for a record by registration number or add a new record. It includes function definitions to get the number of records, search by registration number, and write a new record to the file. The main function uses a switch menu to call the writeRecord and search functions, or exit the program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<stdio.

h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<fstream>

using namespace std;

class student
{
public:char rrn[5],name[15],usn[15],age[5],sem[5],branch[15],buffer[100];
};

student s[100];

int getNumRecords()
{
fstream in;
int cnt=0;
in.open("stu1.txt",ios::in);
if(!in)
{
cout<<"\ncannot open the file in output mode";
return 0;
}

while(!in.eof())
{
in.getline(s[cnt].rrn,5,'|');
in.getline(s[cnt].name,15,'|');
in.getline(s[cnt].usn,15,'|');
in.getline(s[cnt].age,5,'|');
in.getline(s[cnt].sem,5,'|');
in.getline(s[cnt].branch,15,'\n');
cnt++;
}
cnt--;
return cnt;
}

void search()
{
char rrn[5];
int cnt=0;
cout<<"\nEnter the RRN to be searched ";
cin>>rrn;
cnt=getNumRecords();
printf("Name\tUsn\tAge\tSem\tBranch\n");
for(int j=0;j<cnt;j++)
{
if(strcmp(rrn,s[j].rrn)==0)
{
printf("\nRecord found");
printf("\n%s\t%s\t%s\t%s\t
%s",s[j].name,s[j].usn,s[j].age,s[j].sem,s[j].branch);
return;
}
}
cout<<"\nRecord not found";
return;
}

void writeRecord()
{
fstream app;
student s;
char rcnt[5];
int cnt;
app.open("stu1.txt",ios::out|ios::app);
if(!app)
{
cout<<"cannot open the file in output mode";
exit(0);
}
cout<<"\nEnter the student name = ";
cin>>s.name;
cout<<"\nEnter the usn = ";
cin>>s.usn;
cout<<"\nEnter the age = ";
cin>>s.age;
cout<<"\nEnter the sem = ";
cin>>s.sem;
cout<<"\nEnter the branch = ";
cin>>s.branch;
cnt = getNumRecords();
cnt++;
sprintf(rcnt,"%d",cnt);
strcpy(s.buffer,rcnt);
strcat(s.buffer,"|");
strcat(s.buffer,s.name);
strcat(s.buffer,"|");
strcat(s.buffer,s.usn);
strcat(s.buffer,"|");
strcat(s.buffer,s.age);
strcat(s.buffer,"|");
strcat(s.buffer,s.sem);
strcat(s.buffer,"|");
strcat(s.buffer,s.branch);
strcat(s.buffer,"\n");
app<<s.buffer;
app.close();
}

int main()
{
int ch;
for(;;)
{
cout<<"\n0:exit";
cout<<"\n1:Insert";
cout<<"\n2:Search";
cout<<"\nEnter the choice = ";
cin>>ch;
switch(ch)
{
case 1:writeRecord();
break;
case 2:search();
break;
case 0:exit(0);
default:cout<<"\nInvalid option";
}
}

You might also like