This post was published long ago, when I was a student and an amateur blogger. The links might be broken and content may not be useful anymore. Please read this content keeping its age in mind.
#include<iostream.h>
#include<stdlib.h>
#include<fstream.h>
#include<conio.h>
#include<string.h>
class student
{
public: char name[15],usn[15],age[5],sem[5],branch[15],buffer[100];
};
void search()
{
char usn[15];
int i=0;
student s1[100];
cout<<“n Enter the usn to be searched”;
cin>>usn;
fstream in;
in.open(“student.txt”,ios::in);
if(!in)
{
cout<<“n Cannot open the file in output mode”;
getch();
exit(0);
}
i=0;
cout<<“NametUsntAgetSemtBranch”;
while(!in.eof())
{
in.getline(s1[i].name,15,’|’);
in.getline(s1[i].usn,15,’|’);
in.getline(s1[i].age,5,’|’);
in.getline(s1[i].sem,5,’|’);
in.getline(s1[i].branch,15,’n’);
i++;
}
for(int j=0;j<i;j++)
{
if(strcmp(usn,s1[j].usn)==0)
{
cout<<“Record Foundn”;
cout<<“n”<<s1[j].name<<“t”<<s1[j].usn<<“t”<<s1[j].age<<“t”<<s1[j].sem<<“t”<<s1[j].branch;
return;
}
}
cout<<“n Record not found”;
return;
}
void writerecord()
{
fstream app;
student s;
app.open(“student.txt”,ios::app);
if(!app)
{
cout<<” Cannot open the file in output mode”;
getch();
exit(0);
}
cout<<“nenter the student name =”;
cin>>s.name;
cout<<“nEnter the usn =”;
cin>>s.usn;
cout<<“n Enter the age = “;
cin>>s.age;
cout<<“n Enter the sem =” ;
cin>>s.sem;
cout<<“n Enter the branch =”;
cin>>s.branch;
strcpy(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();
}
void main()
{
clrscr();
int ch;
fstream out;
out.open(“student.txt”,ios::out);
if(!out)
{
cout<<“Cannot open the file in output mode”;
getch();
exit(0);
}
out.close();
for(;;)
{
cout<<“n1. Insert”;
cout<<“n2. Search”;
cout<<“n3. Exit”;
cout<<“Enter the choice : “;
cin>>ch;
switch(ch)
{
case 1: writerecord(); break;
case 2: search(); break;
case 3: exit(0);
default: cout<< “n Invalid Choice”;
}
}
}
Sahana says
Hi Sandeep…
Your programs are really good….
Sir don’t mind, i have a suggestion for you,please write program heading while writing the program.If you write only as ‘program 4’ we are confused which programs is this.It was comfortable till program 3,because you have wrote program heading as comment line.Hope you will continue it with program heading…
Thank u sir…
sandeephegde says
Thanks for your compliments.
I shall include the headers for the programs. Due to my hectic schedule , I had posted some post in hurry bury.
Sorry for the inconvinience.
Sahana says
Hi Sandeep…
Your programs are really good….
Sir don’t mind, i have a suggestion for you,please write program heading while writing the program.If you write only as ‘program 4’ we are confused which programs is this.It was comfortable till program 3,because you have wrote program heading as comment line.Hope you will continue it with program heading…
Thank u sir…
sandeephegde says
Thanks for your compliments.
I shall include the headers for the programs. Due to my hectic schedule , I had posted some post in hurry bury.
Sorry for the inconvinience.
Sahana says
Hi Sandeep…
Your programs are really good….
Sir don’t mind, i have a suggestion for you,please write program heading while writing the program.If you write only as ‘program 4’ we are confused which programs is this.It was comfortable till program 3,because you have wrote program heading as comment line.Hope you will continue it with program heading…
Thank u sir…
sandeephegde says
Thanks for your compliments.
I shall include the headers for the programs. Due to my hectic schedule , I had posted some post in hurry bury.
Sorry for the inconvinience.
shaardula says
Hey! alert this is not at all the RRN program.in the rrn prog the storing and retrieval takes place using RRn.the logic is completely different.you get a record by giving it’s rec number.anybody plz don’t do this prog it’s totally wrong.we have to write a prog which holds the recno and their offset as index.we have to use pointers and seek func for that.we have written a completely different easy prog..
sandeephegde says
Dear Shaardula,
There is no sense in just telling you have written a easy program which is perfect ! If so why don’t you share it here instead of just commenting !
Anyway I’m posting another alternative program whic is available at http://www.pingsense.com/2009/06/file-structure-fs-lab-program-4-6th-semester-information-science-alternative/
I do have more programs which are perect in logic , but are more complex. I do not think the logic matters for some people since thety want it easyier and to be execyted in lab. You can execute the perfect ones if you are that intelligent !
shaardula says
Hey! alert this is not at all the RRN program.in the rrn prog the storing and retrieval takes place using RRn.the logic is completely different.you get a record by giving it’s rec number.anybody plz don’t do this prog it’s totally wrong.we have to write a prog which holds the recno and their offset as index.we have to use pointers and seek func for that.we have written a completely different easy prog..
sandeephegde says
Dear Shaardula,
There is no sense in just telling you have written a easy program which is perfect ! If so why don’t you share it here instead of just commenting !
Anyway I’m posting another alternative program whic is available at http://www.pingsense.com/2009/06/file-structure-fs-lab-program-4-6th-semester-information-science-alternative/
I do have more programs which are perect in logic , but are more complex. I do not think the logic matters for some people since thety want it easyier and to be execyted in lab. You can execute the perfect ones if you are that intelligent !
shaardula says
Hey! alert this is not at all the RRN program.in the rrn prog the storing and retrieval takes place using RRn.the logic is completely different.you get a record by giving it’s rec number.anybody plz don’t do this prog it’s totally wrong.we have to write a prog which holds the recno and their offset as index.we have to use pointers and seek func for that.we have written a completely different easy prog..
sandeephegde says
Dear Shaardula,
There is no sense in just telling you have written a easy program which is perfect ! If so why don’t you share it here instead of just commenting !
Anyway I’m posting another alternative program whic is available at http://www.pingsense.com/2009/06/file-structure-fs-lab-program-4-6th-semester-information-science-alternative/
I do have more programs which are perect in logic , but are more complex. I do not think the logic matters for some people since thety want it easyier and to be execyted in lab. You can execute the perfect ones if you are that intelligent !
shaardula says
There’s no question of intelligence here.it’s the question of correctness.IF you do a program which is simple but not even for what was asked you get no marks.Any way see the program below to believe -i am not telling some bluff ok..(and for the link you have given it’s not working..
)the below prog does both jobs -has variable length and also gives RRN access.even has sample o/p.
#include
#include
#include
#include
#include
#include
int count;
class variable
{
struct student
{ char usn[20],name[20],sem[20],dept[20];
};
public: void pack();
void unpack();
};
void variable::pack()
{ char b[100],c[20];
student s;
cout<>s.usn>>s.name>>s.sem>>s.dept;
ofstream ofile;
ofile.open(“student3.txt”,ios::app);
ofile.seekp(0,ios::end);
long p=ofile.tellp();
sprintf(c,”|%d|%ld|$”,count+1,p);
sprintf(b,”|%s|%s|%s|%s|$”,s.usn,s.name,s.sem,s.dept);
count++;
ofile<<b;
ofstream rr;
rr.open(“rr.txt”,ios::app);
rr<<c;
rr.close();
ofile.close();
}
void variable::unpack()
{ char b[100],temp[100],c[20],t[50];
student s;
int rrn,frrn,n=0,flag=0;
long pos;
cout<>rrn;
ifstream rr;
rr.open(“rr.txt”,ios::out);
rr.seekg(0,ios::beg);
ifstream ifile;
ifile.open(“student3.txt”,ios::out);
ifile.seekg(0,ios::beg);
while(n<=count)
{
rr.getline(c,20,’$’);
sscanf(c,”|%d|%ld|$”,&frrn,&pos,t);
if(rrn==frrn)
{ flag=1;
ifile.seekg(pos,ios::beg);
cout<<“nDatabase: n”;
ifile.getline(b,100,’$’);
sscanf(b,”|%[^|]|%[^|]|%[^|]|%[^|]|$”,s.usn,s.name,s.sem,s.dept,temp);
cout<<s.usn<<” “<<s.name<<” “<<s.sem<<” “<<s.dept<<endl;
break;
}
n++;
}
if(flag==0) cout<<“nKey not found”;
rr.close();
ifile.close();
}
void main()
{
variable f;
clrscr();
printf(“nPROGRAM TO RETRIEVE RECORDS USING RRNn”);
char buffer[100];
ifstream ifile;
ifile.open(“student3.txt”,ios::out);
ifile.seekg(0,ios::beg);
while(!ifile.eof())
{ ifile.getline(buffer,100,’$’);
count++;
}
count–;
ifile.close();
int ch;
for(;;)
{ cout<>ch;
switch(ch)
{ case 1: f.pack();
break;
case 2: f.unpack();
break;
default: exit(0);
}
}
}
/* OUTPUT
PROGRAM TO RETRIEVE RECORDS USING RRN
1: pack 2: unpack 3:Exit
Enter your choice: 1
Enter usn, name, sem, dept: 12 ram 4 is
1: pack 2: unpack 3:Exit
Enter your choice: 1
Enter usn, name, sem, dept: 23 sam 4 cse
1: pack 2: unpack 3:Exit
Enter your choice: 1
Enter usn, name, sem, dept: 1 ali 6 iem
1: pack 2: unpack 3:Exit
Enter your choice: 2
Enter record no: 3
Database:
1 ali 6 iem
1: pack 2: unpack 3:Exit
Enter your choice: 2
Enter record no: 1
Database:
12 ram 4 is
1: pack 2: unpack 3:Exit
Enter your choice: 3
*/
shaardula says
There’s no question of intelligence here.it’s the question of correctness.IF you do a program which is simple but not even for what was asked you get no marks.Any way see the program below to believe -i am not telling some bluff ok..(and for the link you have given it’s not working..
)the below prog does both jobs -has variable length and also gives RRN access.even has sample o/p.
#include
#include
#include
#include
#include
#include
int count;
class variable
{
struct student
{ char usn[20],name[20],sem[20],dept[20];
};
public: void pack();
void unpack();
};
void variable::pack()
{ char b[100],c[20];
student s;
cout<>s.usn>>s.name>>s.sem>>s.dept;
ofstream ofile;
ofile.open(“student3.txt”,ios::app);
ofile.seekp(0,ios::end);
long p=ofile.tellp();
sprintf(c,”|%d|%ld|$”,count+1,p);
sprintf(b,”|%s|%s|%s|%s|$”,s.usn,s.name,s.sem,s.dept);
count++;
ofile<<b;
ofstream rr;
rr.open(“rr.txt”,ios::app);
rr<<c;
rr.close();
ofile.close();
}
void variable::unpack()
{ char b[100],temp[100],c[20],t[50];
student s;
int rrn,frrn,n=0,flag=0;
long pos;
cout<>rrn;
ifstream rr;
rr.open(“rr.txt”,ios::out);
rr.seekg(0,ios::beg);
ifstream ifile;
ifile.open(“student3.txt”,ios::out);
ifile.seekg(0,ios::beg);
while(n<=count)
{
rr.getline(c,20,’$’);
sscanf(c,”|%d|%ld|$”,&frrn,&pos,t);
if(rrn==frrn)
{ flag=1;
ifile.seekg(pos,ios::beg);
cout<<“nDatabase: n”;
ifile.getline(b,100,’$’);
sscanf(b,”|%[^|]|%[^|]|%[^|]|%[^|]|$”,s.usn,s.name,s.sem,s.dept,temp);
cout<<s.usn<<” “<<s.name<<” “<<s.sem<<” “<<s.dept<<endl;
break;
}
n++;
}
if(flag==0) cout<<“nKey not found”;
rr.close();
ifile.close();
}
void main()
{
variable f;
clrscr();
printf(“nPROGRAM TO RETRIEVE RECORDS USING RRNn”);
char buffer[100];
ifstream ifile;
ifile.open(“student3.txt”,ios::out);
ifile.seekg(0,ios::beg);
while(!ifile.eof())
{ ifile.getline(buffer,100,’$’);
count++;
}
count–;
ifile.close();
int ch;
for(;;)
{ cout<>ch;
switch(ch)
{ case 1: f.pack();
break;
case 2: f.unpack();
break;
default: exit(0);
}
}
}
/* OUTPUT
PROGRAM TO RETRIEVE RECORDS USING RRN
1: pack 2: unpack 3:Exit
Enter your choice: 1
Enter usn, name, sem, dept: 12 ram 4 is
1: pack 2: unpack 3:Exit
Enter your choice: 1
Enter usn, name, sem, dept: 23 sam 4 cse
1: pack 2: unpack 3:Exit
Enter your choice: 1
Enter usn, name, sem, dept: 1 ali 6 iem
1: pack 2: unpack 3:Exit
Enter your choice: 2
Enter record no: 3
Database:
1 ali 6 iem
1: pack 2: unpack 3:Exit
Enter your choice: 2
Enter record no: 1
Database:
12 ram 4 is
1: pack 2: unpack 3:Exit
Enter your choice: 3
*/
shaardula says
There’s no question of intelligence here.it’s the question of correctness.IF you do a program which is simple but not even for what was asked you get no marks.Any way see the program below to believe -i am not telling some bluff ok..(and for the link you have given it’s not working..
)the below prog does both jobs -has variable length and also gives RRN access.even has sample o/p.
#include
#include
#include
#include
#include
#include
int count;
class variable
{
struct student
{ char usn[20],name[20],sem[20],dept[20];
};
public: void pack();
void unpack();
};
void variable::pack()
{ char b[100],c[20];
student s;
cout<>s.usn>>s.name>>s.sem>>s.dept;
ofstream ofile;
ofile.open(“student3.txt”,ios::app);
ofile.seekp(0,ios::end);
long p=ofile.tellp();
sprintf(c,”|%d|%ld|$”,count+1,p);
sprintf(b,”|%s|%s|%s|%s|$”,s.usn,s.name,s.sem,s.dept);
count++;
ofile<<b;
ofstream rr;
rr.open(“rr.txt”,ios::app);
rr<<c;
rr.close();
ofile.close();
}
void variable::unpack()
{ char b[100],temp[100],c[20],t[50];
student s;
int rrn,frrn,n=0,flag=0;
long pos;
cout<>rrn;
ifstream rr;
rr.open(“rr.txt”,ios::out);
rr.seekg(0,ios::beg);
ifstream ifile;
ifile.open(“student3.txt”,ios::out);
ifile.seekg(0,ios::beg);
while(n<=count)
{
rr.getline(c,20,’$’);
sscanf(c,”|%d|%ld|$”,&frrn,&pos,t);
if(rrn==frrn)
{ flag=1;
ifile.seekg(pos,ios::beg);
cout<<“nDatabase: n”;
ifile.getline(b,100,’$’);
sscanf(b,”|%[^|]|%[^|]|%[^|]|%[^|]|$”,s.usn,s.name,s.sem,s.dept,temp);
cout<<s.usn<<” “<<s.name<<” “<<s.sem<<” “<<s.dept<<endl;
break;
}
n++;
}
if(flag==0) cout<<“nKey not found”;
rr.close();
ifile.close();
}
void main()
{
variable f;
clrscr();
printf(“nPROGRAM TO RETRIEVE RECORDS USING RRNn”);
char buffer[100];
ifstream ifile;
ifile.open(“student3.txt”,ios::out);
ifile.seekg(0,ios::beg);
while(!ifile.eof())
{ ifile.getline(buffer,100,’$’);
count++;
}
count–;
ifile.close();
int ch;
for(;;)
{ cout<>ch;
switch(ch)
{ case 1: f.pack();
break;
case 2: f.unpack();
break;
default: exit(0);
}
}
}
/* OUTPUT
PROGRAM TO RETRIEVE RECORDS USING RRN
1: pack 2: unpack 3:Exit
Enter your choice: 1
Enter usn, name, sem, dept: 12 ram 4 is
1: pack 2: unpack 3:Exit
Enter your choice: 1
Enter usn, name, sem, dept: 23 sam 4 cse
1: pack 2: unpack 3:Exit
Enter your choice: 1
Enter usn, name, sem, dept: 1 ali 6 iem
1: pack 2: unpack 3:Exit
Enter your choice: 2
Enter record no: 3
Database:
1 ali 6 iem
1: pack 2: unpack 3:Exit
Enter your choice: 2
Enter record no: 1
Database:
12 ram 4 is
1: pack 2: unpack 3:Exit
Enter your choice: 3
*/
sandeephegde says
Thank you 🙂
sandeephegde says
Thank you 🙂
sandeephegde says
Thank you 🙂