Skip to main content

10.file

File: /home/saiesh/ADS/Assignment No 10/ads10.cpp
Page 1 of 5
#include<iostream>
#include<iomanip>
#include<cstring>
#include<fstream>
#include<stdlib.h>
using namespace std;
class empclass
{
typedef struct employee
{
int empid;
int salary;
char name[30];
}rec;
typedef struct index
{
int empid;
int position;
}indrec;
rec records;
indrec indrecords;
public:
empclass();
void create();
void display();
void addrec();
void delrec();
void modrec();
};
empclass::empclass()
{
strcpy(records.name,"-");
records.empid=-1;
records.salary=-1;
}
void empclass::create()
{
int i,n;
char ans;
fstream seqfile;
fstream indexfile;
seqfile.open("emp.dat",ios::in|ios::out|ios::binary);
indexfile.open("ind.dat",ios::in|ios::out|ios::binary);
indexfile.seekg(0,ios::beg);
seqfile.seekg(0,ios::beg);
cout<<"\nHow many records do you want to enter? ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\nID\t: ";
cin>>records.empid;
cout<<"Name\t: ";
cin>>records.name;
cout<<"Salary\t: ";
cin>>records.salary;
seqfile.write((char*)&records,sizeof(records))<<flush;
indrecords.empid=records.empid;
indrecords.position=i;
indexfile.write((char*)&indrecords,sizeof(indrecords))<<flush;
}
seqfile.close();
indexfile.close();
}File: /home/saiesh/ADS/Assignment No 10/ads10.cpp
void empclass::display()
{
int i=0;
fstream seqfile;
fstream indexfile;
seqfile.open("emp.dat",ios::in);
indexfile.open("ind.dat",ios::in);
indexfile.seekg(0,ios::beg);
while(indexfile.read((char*)&indrecords,sizeof(indrecords)))
{
i=indrecords.position*sizeof(records);
seqfile.seekg(i,ios::beg);
seqfile.read((char*)&records,sizeof(records));
if((records.empid)!=-1)
{
cout<<"\nID\t: "<<records.empid;
cout<<"\nName\t: "<<records.name;
cout<<"\nSalary\t: "<<records.salary<<endl;
}
}
seqfile.close();
indexfile.close();
}
void empclass::addrec()
{
int i=0;
fstream seqfile;
fstream indexfile;
seqfile.open("emp.dat",ios::in|ios::app|ios::out|ios::binary);
indexfile.open("ind.dat",ios::in|ios::app|ios::out|ios::binary);
indexfile.seekg(0,ios::end);
i=indexfile.tellg()/sizeof(indrecords);
cout<<"\nID\t: ";
cin>>records.empid;
cout<<"Name\t: ";
cin>>records.name;
cout<<"Salary\t: ";
cin>>records.salary;
seqfile.write((char*)&records,sizeof(records))<<flush;
indrecords.empid=records.empid;
indrecords.position=i;
indexfile.write((char*)&indrecords,sizeof(indrecords))<<flush;
seqfile.close();
indexfile.close();
}
void empclass::delrec()
{
int id,pos=-1,offset;
fstream seqfile;
fstream indexfile;
seqfile.open("emp.dat",ios::in|ios::out|ios::binary);
indexfile.open("ind.dat",ios::in|ios::out|ios::binary);
cout<<"\nEnter ID to delete: ";
cin>>id;
seqfile.seekg(0,ios::beg);
indexfile.seekg(0,ios::beg);
while(indexfile.read((char*)&indrecords,sizeof(indrecords)))
{
if((indrecords.empid)==id)
{
pos=indrecords.position;
break;
}
Page 2 of 5File: /home/saiesh/ADS/Assignment No 10/ads10.cpp
Page 3 of 5
}
if(pos==-1)
cout<<"\nRecord not found!\n";
else
{
offset=pos*sizeof(records);
seqfile.seekg(offset,ios::beg);
records.empid=-1;
records.salary=-1;
strcpy(records.name,"-");
seqfile.write((char*)&records,sizeof(records))<<flush;
indrecords.empid=-1;
indrecords.position=pos;
offset=pos*sizeof(indrecords);
indexfile.seekg(offset,ios::beg);
indexfile.write((char*)&indrecords,sizeof(indrecords))<<flush;
cout<<"Record Deleted!\n";
}
seqfile.close();
indexfile.close();
}
void empclass::modrec()
{
int id,pos=-1,offset=-1;
fstream seqfile;
fstream indexfile;
seqfile.open("emp.dat",ios::in|ios::out|ios::binary);
indexfile.open("ind.dat",ios::in|ios::out|ios::binary);
cout<<"\nEnter ID to modify: ";
cin>>id;
seqfile.seekg(0,ios::beg);
indexfile.seekg(0,ios::beg);
while(indexfile.read((char*)&indrecords,sizeof(indrecords)))
{
if((indrecords.empid)==id)
{
pos=indrecords.position;
break;
}
}
if(pos==-1)
cout<<"\nRecord not found!\n";
else
{
offset=pos*sizeof(records);
seqfile.seekg(offset,ios::beg);
cout<<"\nName\t: ";
cin>>records.name;
cout<<"Salary\t: ";
cin>>records.salary;
records.empid=id;
seqfile.write((char*)&records,sizeof(records))<<flush;
indrecords.empid=id;
indrecords.position=pos;
offset=pos*sizeof(indrecords);
indexfile.seekg(offset,ios::beg);
indexfile.write((char*)&indrecords,sizeof(indrecords))<<flush;
cout<<"Record Modified!\n";
}
seqfile.close();
indexfile.close();
}
int main()
{File: /home/saiesh/ADS/Assignment No 10/ads10.cpp
empclass obj;
int ch;
while(1)
{
cout<<"\n\tMENU";
cout<<"\n1.Create Database";
cout<<"\n2.Display Database";
cout<<"\n3.Add record";
cout<<"\n4.Delete a record";
cout<<"\n5.Modify a record";
cout<<"\n6.Exit";
cout<<"\nEnter your choice: ";
cin>>ch;
switch(ch)
{
case 1: obj.create();
break;
case 2: obj.display();
break;
case 3: obj.addrec();
break;
case 4: obj.delrec();
break;
case 5: obj.modrec();
break;
case 6: exit(0);
}
}
return(0);
}
/*
OUTPUT
saiesh@saiesh :~$ g++ ads10.cpp
saiesh@saiesh :~$ ./a.out
MENU
1.Create Database
2.Display Database
3.Add record
4.Delete a record
5.Modify a record
6.Exit
Enter your choice: 1
How many records do you want to enter? 4
ID
Name
Salary : 1
: Saiesh
: 10000
ID
Name
Salary : 2
: Rohan
: 8000
ID
Name
Salary : 3
: Misba
: 7500
ID
Name
Salary : 4
: Payal
: 7000
MENU
1.Create Database
2.Display Database
Page 4 of 5File: /home/saiesh/ADS/Assignment No 10/ads10.cpp
3.Add record
4.Delete a record
5.Modify a record
6.Exit
Enter your choice: 2
ID
Name
Salary : 1
: Saiesh
: 10000
ID
Name
Salary : 2
: Rohan
: 8000
ID
Name
Salary : 3
: Misba
: 7500
ID
Name
Salary : 4
: Payal
: 7000
MENU
1.Create Database
2.Display Database
3.Add record
4.Delete a record
5.Modify a record
6.Exit
Enter your choice: 3
ID
Name
Salary
: 5
: Karishma
: 6000
MENU
1.Create Database
2.Display Database
3.Add record
4.Delete a record
5.Modify a record
6.Exit
Enter your choice: 4
Enter ID to delete: 4
Record Deleted!
MENU
1.Create Database
2.Display Database
3.Add record
4.Delete a record
5.Modify a record
6.Exit
Enter your choice: 5
Enter ID to modify: 5
Name
: Karishma
Salary : 7000
Record Modified!
saiesh@saiesh :~$ */
Page 5 of 5

Comments

Popular posts from this blog

PPL 1

NEW Unit III STRUCTURING OF PROGRAM MULTIPLE CHOICE QUESTIONS 1. Which of the following is the functionality of ‘Data Abstraction’? (a) Reduce Complexity (b) Binds together code and data (c) Parallelism (d) None of the mentioned Answer : a Explanation : An essential element of Object Oriented Programming is ‘Data Abstraction’ which means hiding things. Complexity is managed through abstraction. 2. Which of the following mechanisms is/are provided by Object Oriented Language to implement Object Oriented Model? (a) Encapsulation (b) Inheritance (c) Polymorphism (d) All of the mentioned Answer : d Explanation : None. 3. Which of these is the functionality of ‘Encapsulation’? (a) Binds together code and data (b) Using single interface for general class of actions. (c) Reduce Complexity (d) All of the mentioned Answer : a Explanation : ‘Encapsulation’ acts as protective wrapper that prevents code and data from being accessed by other code defined outside the...

EEE

EEE Unit-1 DC Machines MCQ's MULTIPLE CHOICE QUESTIONS ON UNIT 1: DC MACHINES Edit 1.The sole purpose of a Commutator in a dc generator is to------- ((A))increase output voltage ((B)) reduce sparking at brushes ((C)) provide smoother output ((D)) convert the induced ac into dc Hide ! Answer : D 2.In a dc generator, the generated emf is directly proportional to the------ ((A)) field current ((B)) pole flux ((C)) number of armature parallel paths ((D)) number of dummy coils Hide ! Answer:B 3.An ideal dc generator has .......... voltage regulation. ((A))low ((B))zero ((C))positive ((D))negative Hide ! Answer:B 4.Which generator may have poorest voltage regulation? ((A))series ((B))shunt((C))compound ((D))high Hide ! Answer:A 5.Voltage equation of a dc motor is----- ((A)) V = Eb + IaRa ((B)) Eb = V + IaRa ((C))V = Eb /IaRa ((D))V = Eb + Ia2Ra Hide ! Answer: A 6.Which of the following motor has the constant speed? ((A))Series motor ((B))Shunt motor ((C))Cumulatively compound motor ((D)...

replacing strings without using inbuild functions

 Q:replacing strings without using inbuild functions. We are using gcc compiler and terminal to compile the code and ubuntu OS. PROGRAM: #include<stdio.h> int strln1(char s1[]); int strln2(char s2[]); void strcpy1(char s1[],char s2[]); int main() {     char s1[30],s2[30];     int result1,result2;     printf("Enter the string s1");     scanf("%s",s1);     printf("Enter the string s2");     scanf("%s",s2);     printf("Your string s1 is %s",s1);     printf("\nYour string s2 is %s",s2);     result1=strln1(s1);     result2=strln2(s2);     printf("\nlength of string s1 is %d\n",result1);     printf("\nlength of string s2 is %d\n",result2);     strcpy1(s1,s2); } int strln1(char s1[]) {     int i,ln;   ...