Program 6
Program 6
#include<fstream>
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string>
#define datafile "stud6.txt"
#define indexfile "pri6.txt"
#define sindexfile "sec6.txt"
using namespace std;
class student
{
char dusn[15], name[20], branch[6], sem[5];
public:
void add();
void pack();
friend int search(char*);
void remove();
void datadisp();
void unpack();
void ssearch();
}s;
class index
{
public:
char iusn[15], addr[5];
void initial();
void write();
}id[50], in;
class sindex
{
public:
char sname[20], susn[15];
void sinitial();
void swrite();
}sind, sid[50];
void index::initial()
{
ifile.open(indexfile, ios::in);
if (!ifile)
{
indsize = 0;
return;
}
for (indsize = 0;; indsize++)
{
ifile.getline(id[indsize].iusn, 15, '|');
ifile.getline(id[indsize].addr, 5, '\n');
if (ifile.eof())
break;
}
ifile.close();
}
//function to pack
void student::pack()
{
strcpy_s(buffer, dusn); strcat_s(buffer, "|");
strcat_s(buffer, name); strcat_s(buffer, "|");
strcat_s(buffer, branch); strcat_s(buffer, "|");
strcat_s(buffer, sem);
}
if (strcmp(sid[spos].sname, skey) == 0)
{
pos = search(rusn);
dfile.seekp(atoi(id[pos].addr), ios::beg);
dfile.put('$');
for (i = pos; i < indsize; i++)
id[i] = id[i + 1];
indsize--;
for (i = spos; i < sindsize; i++)
sid[i] = sid[i + 1];
sindsize--;
}
else
cout << "usn number and name doesnot match";
}
void student::ssearch()
{
int pos, flag = -1;
cout << "\nenter the name to search(sec key):";
cin >> skey;
opener(dfile, datafile, ios::in | ios::out);
//main program
void main()
{
int ch, flag;
in.initial();
sind.sinitial();
for (;;)
{
cout << endl << "1-Add,2-display,3-search,4-delete,5-exit\n";
cin >> ch;
switch (ch)
{
case 1: cout << endl << "enter student details : " << endl;
s.add();
in.write();
sind.swrite();
break;
case 2: opener(dfile, datafile, ios::in);
cout << "\nthe indexfile, secondary file and data file" << endl;
s.datadisp();
break;
case 3:cout << "To search based on sec key ";
s.ssearch();
break;
case 4: flag = sec_search();
if (flag == -1)
cout << "no data record found";
else
{
s.remove();
in.write();
sind.swrite();
}
break;
default: exit(0);
}
dfile.close();
ifile.close();
sifile.close();
}
}