Вы находитесь на странице: 1из 19

30.

MENU DRIVEN DATA STRUCTURES PROGRAM


#include <fstream.h>
#include <conio.h>
#include <stdio.h>
#include <iomanip.h>
#include <string.h>
#include <dos.h>
#include <process.h>

class student
{
public:
int roll;
char name[20];
int marks;

void input()
{
cout << "\nEnter Roll No: ";
cin >> roll;

cout << "Enter Name: ";


gets(name);

cout << "Enter Marks: ";


cin >> marks;
}

void display()
{
cout << setw (7) << roll << setw (10) << name << setw (8) <<
marks << endl;
}

};

void main()
{
clrscr();

char fname[20], str[50], ch, str1[20];


long pos;
int n1, n2, n, i=0;
student s1, s2, s3[11], temp;

fstream forg, fnew;

cout << " MENU\n";


cout << "\n1. Create A File"
<< "\n2. Append To An Existing File"
<< "\n3. Read A File"
<< "\n4. Copy A File To Another"
<< "\n5. Search in A File"
<< "\n6. Modification"
<< "\n7. Delete A Record"
<< "\n8. Insert In A Record"
<< "\n9. Sort A File"
<< "\n10. Rename A File";

cout << "\n\nYour Choice: ";

cin >> n1;

switch (n1)
{
case 1: clrscr();
cout << "1. Text File"
<< "\n2. Binary File";

cout << "\n\nYour Choice: ";


cin >> n2;

switch (n2)
{
case 1: cout << "Enter Name Of File (Add '.txt' at end): ";
gets(fname);

forg.open (fname, ios::out | ios::noreplace);

do
{
cout << "Enter Text: ";

gets (str);

forg << str;

cout << "Want To Enter More? (Y/N): ";


cin >> ch;
} while (ch == 'Y' || ch == 'y');

forg.close();
getch();

break;

case 2: cout << "Enter Name Of File (Add '.dat' at end): ";
gets(fname);

forg.open (fname, ios::out | ios::binary |


ios::noreplace);

do
{
s1.input();

forg.write((char *) &s1, sizeof(s1));

cout << "\nWant To Add More? (Y/N): ";


cin >> ch;

} while (ch == 'y' || ch == 'Y');

forg.close();
getch();

break;

default: exit(0);
}

break;

case 2: clrscr();
cout << "1. Text File"
<< "\n2. Binary File";

cout << "\n\nYour Choice: ";


cin >> n2;

switch (n2)
{
case 1: cout << "Enter Name Of File (Add '.txt' at end): ";
gets(fname);

forg.open (fname, ios::out | ios::app | ios::nocreate);

if (!forg)
{
cout << "File Doesnt Exist!!";
getch();
break;
}

do
{
cout << "Enter Text: ";

gets (str);

forg << str;

cout << "Want To Enter More? (Y/N): ";


cin >> ch;
} while (ch == 'Y' || ch == 'y');

forg.close();
getch();

break;

case 2: cout << "Enter Name Of File (Add '.dat' at end): ";
gets(fname);

forg.open (fname, ios::out | ios::binary | ios::app |


ios::nocreate);

if (!forg)
{
cout << "File Doesnt Exist!!";
getch();
break;
}

do
{
s1.input();

forg.write((char *) &s1, sizeof(s1));

cout << "\nWant To Add More? (Y/N): ";


cin >> ch;

} while (ch == 'y' || ch == 'Y');

forg.close();
getch();

break;

default: exit(0);
}

break;

case 3: clrscr();

cout << "Enter Name Of File You Want To Read From: ";
gets(fname);

forg.open (fname , ios::in | ios::binary);

cout << setw (7) << "Roll No" << setw (10) << "Name" << setw
(8) << "Marks" << endl;

while (forg.read ((char *) &s1, sizeof(s1)))


{
s1.display();
}

getch();
break;

case 4: clrscr();

cout << "Enter Name Of File You Want To Copy FROM: ";
gets(fname);

forg.open (fname, ios::in | ios::binary);

if(!forg)
{
cout << "File Doesnt Exists!!";
getch();
break;
}

cout << "Enter Name Of File You Want To Copy TO: ";
gets(fname);

fnew.open (fname, ios::out | ios::binary | ios::noreplace);

if(!fnew)
{
cout << "File Name Already Exists!!";
getch();
break;
}

while (forg.read((char*) &s1, sizeof(s1)))


{
fnew.write ((char*) &s1, sizeof(s1));
}

forg.close();
fnew.close();

getch();

break;

case 5: cout << "Enter The Name Of The File (Add '.dat'): ";
gets(fname);

forg.open (fname, ios::in | ios::binary);

if (!forg)
{
cout << "File Doesnt Exist!!";
getch();
break;
}

cout << "Okay!";


delay (500);

clrscr();

cout << "1. By Name"


<< "\n2. By Roll Number";

cout << "\n\nYour Choice: ";


cin >> n2;

switch (n2)
{
case 1: clrscr();
cout << "Enter Name You Want To Search: ";

gets (str1);

cout << setw (7) << "Roll No" << setw (10) << "Name" <<
setw (8) << "Marks" << endl;

while (forg.read ((char*) &s1, sizeof(s1)))


{
if (strcmpi (str1, s1.name) == 0)
{
s1.display();
break;
}
}

break;

case 2: clrscr();
cout << "Enter Roll Number You Want To Search: ";
cin >> n;

cout << setw (7) << "Roll No" << setw (10) << "Name" <<
setw (8) << "Marks" << endl;

while (forg.read ((char*) &s1, sizeof(s1)))


{
if (s1.roll == n)
{
s1.display();
break;
}
}

break;
}
forg.close();

break;

case 6: clrscr();
cout << "Enter The Name Of The File (Add '.dat'): ";
gets(fname);

forg.open (fname, ios::in | ios::out | ios::binary);

cout << "Okay!";


delay (500);

forg.close();

clrscr();

cout << "1. By Name"


<< "\n2. By Roll Number"
<< "\n3. By Record Number";

cout << "\n\nYour Choice: ";


cin >> n2;

cout << "\n\n";

do
{
forg.open (fname, ios::in | ios::out | ios::binary);

switch (n2)
{
case 1: cout << "Enter Name You Want To Edit: ";
gets(str);

while (forg.read ((char *) &s1, sizeof(s1)))


{
if (strcmpi(s1.name, str) == 0)
{
pos = forg.tellg() - sizeof(s1);
break;
}
}
break;

case 2: cout << "Enter Roll number You Want To Edit: ";
cin >> n;

while (forg.read ((char *) &s1, sizeof(s1)))


{
if (s1.roll == n)
{
pos = forg.tellg() - sizeof(s1);
break;
}
}
break;
case 3: cout << "Enter Record Number You Want To Modify With:
";
cin >> n;

pos = (n-1) * sizeof(s1);

break;

forg.seekg(pos);

s1.input();

forg.write ((char*)&s1, sizeof(s1));

clrscr();

cout << "New Data: \n\n\n";

cout << setw (7) << "Roll No" << setw (10) << "Name" << setw (8)
<< "Marks" << endl;

forg.close();

forg.open (fname, ios::in | ios::binary);

while (forg.read ((char*) &s1, sizeof(s1)))


{
s1.display();
}

forg.close();

cout << "\n\nWanna Modify More: ";


cin >> ch;

} while (ch == 'y' || ch == 'Y');

break;

case 7: clrscr();

cout << "Enter The Name Of The File (Add '.dat'): ";
gets(fname);

forg.open (fname, ios::in | ios::out | ios::binary);


fnew.open ("newstudent.dat", ios::out | ios::binary);

cout << "Okay!";


delay (500);

clrscr();
cout << "1. By Name"
<< "\n2. By Roll Number"
<< "\n3. By Record Number";

cout << "\n\nYour Choice: ";


cin >> n2;

cout << "\n\n";

switch (n2)
{
case 1: cout << "Enter Name You Want To Delete: ";
gets(str);

while (forg.read((char*) &s1, sizeof(s1)))


{
if (strcmpi(str, s1.name) != 0)
{
fnew.write ((char*) &s1, sizeof(s1));
}
}
break;

case 2: cout << "Enter Roll Number Of Record You Want To


Delete: ";
cin >> n;

while (forg.read((char*) &s1, sizeof(s1)))


{
if (s1.roll != n)
{
fnew.write ((char*) &s1, sizeof(s1));
}
}
break;

case 3: cout << "Enter The Record Number You Want To Delete:
";
cin >> n;

while (forg.read((char*) &s1, sizeof(s1)))


{
i++;

if (i != n)
{
fnew.write ((char*) &s1, sizeof(s1));
}
}

break;

}
forg.close();
fnew.close();

remove (fname);

rename ("newstudent.dat" , fname);

cout << "\n\nNew Data:\n\n ";

cout << setw (7) << "Roll No" << setw (10) << "Name" << setw
(8) << "Marks" << endl;

forg.open (fname, ios::in | ios::binary);

while (forg.read ((char*) &s1, sizeof(s1)))


{
s1.display();
}

forg.close();

getch();

break;

case 8: clrscr();

cout << "Enter The Name Of The File (Add '.dat'): ";
gets(fname);

forg.open (fname, ios::in | ios::out | ios::binary);


fnew.open ("newstudent.dat", ios::out | ios::binary);

/* cout << "Okay!";


delay (500);

clrscr();

cout << "1. By Name"


<< "\n2. By Roll Number"
<< "\n3. By Record Number";

cout << "\n\nYour Choice: ";


cin >> n2;

cout << "\n\n";

switch (n2)
{ */

cout << "Enter The Roll Number After Which You Want To Add: ";
cin >> n;

while (forg.read ((char*) &s1, sizeof(s1)))


{
fnew.write ((char*)&s1, sizeof(s1));

if (s1.roll == n)
{
s2.input();

fnew.write ((char*) &s2, sizeof(s2));


}
}

forg.close();
fnew.close();

remove (fname);

rename ("newstudent.dat" , fname);

cout << "\n\nNew Data:\n\n ";

cout << setw (7) << "Roll No" << setw (10) << "Name" << setw
(8) << "Marks" << endl;

forg.open (fname, ios::in | ios::binary);

while (forg.read ((char*) &s1, sizeof(s1)))


{
s1.display();
}

forg.close();

getch();

break;

case 9: clrscr();

cout << "Enter The Name Of The File (Add '.dat'): ";
gets(fname);

forg.open (fname, ios::in | ios::out | ios::binary);

while (forg.read ((char *) &s1, sizeof(s1)))


{
s3[i] = s1;
i++;
}

for (int j=0; j < i; j++)


{
for (int k = 0; k < i+1; k++)
{
if (s3[j].roll < s3[k].roll)
{
temp = s3[k];
s3[k] = s3[j];
s3[j] = temp;
}
}
}

cout << setw (7) << "Roll No" << setw (10) << "Name" << setw
(8) << "Marks" << endl;

forg.close(
);

forg.open (fname, ios::out | ios::binary);

for (j=0; j<i; j++)


{
forg.write ((char*) &s3[j], sizeof(s3[j]));
s3[j].display();
}

forg.close();

getch();

break;

case 10 : clrscr();

cout << "Enter Original File Name: ";


gets(str);

cout << "Enter New Name: ";


gets(str1);

rename (str, str1);

getch();

break;

default: exit(0);

getch();
}

Вам также может понравиться