1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
#include<stdlib.h>
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
class student
{
public: char name[10];
char usn[10];
char age[5];
char sem[5];
char branch[5];
char buffer[45];
};
fstream file;
student s;
void pack()
{
int k,n;
printf("how many records\n");
scanf("%d",&n);
for(k=0;k<n;k++)
{
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;
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);
int count=strlen(s.buffer);
for(int k=0;k<45-count;k++)
strcat(s.buffer,"!");
strcat(s.buffer,"\n");
file<<s.buffer;
}
file.close();
}
void writerecord()
{
file.open("p2.txt",ios::out);
if(!file)
{
cout<<"cannot open the file in out mode";
exit(1);
}
pack();
}
void unpack()
{
file.getline(s.name,10,'|');
file.getline(s.usn,10,'|');
file.getline(s.age,5,'|');
file.getline(s.sem,5,'|');
file.getline(s.branch,5,'!');
}
void search()
{
char usn[10];
char extra[45];
file.open("p2.txt",ios::in);
if(!file)
{
cout<<"\nunable to open the file in read mode";
exit(0);
}
cout<<"\nenter the record's usn you want to search = ";
cin>>usn;
while(!file.eof())
{
unpack();
file.getline(extra,45,'\n');
if(strcmp(s.usn,usn)==0)
{
cout<<"\nrecord found";
cout<<"\n"<<s.name<<"\t"<<s.usn<<"\t"<<s.age<<"\t"<<s.sem<<"\t"<<s.branch;
file.close();
return;
}
}
cout<<"\nrecord not found";
file.close();
}
void displayFile()
{
int i;
char extra[45];
file.open("p2.txt",ios::in);
if(!file)
{
cout<<"\ncannot open the file in read mode";
exit(1);
}
i=0;
cout<<"\n\nNAME\t\tUSN\t\tAGE\t\tSEM\t\tBRANCH\n";
cout<<"----\t\t---\t\t---\t\t---\t\t------\n";
while(!file.eof())
{
unpack();
file.getline(extra,45,'\n');
cout<<"\n"<<s.name<<"\t"<<s.usn<<"\t"<<s.age<<"\t"<<s.sem<<"\t"<<s.branch<<"\n";
}
file.close();
}
void modify()
{
char usn[10];
char buffer[45];
char extra[45];
int i;
int j;
student s[20];
file.open("p2.txt",ios::in);
if(!file)
{
cout<<"\nunable to open the file in input mode";
exit(1);
}
cout<<"\nenter the usn of the record to be modified\n";
cin>>usn;
cout<<"\n";
i=0;
while(!file.eof())
{
file.getline(s[i].name,10,'|');
file.getline(s[i].usn,10,'|');
file.getline(s[i].age,5,'|');
file.getline(s[i].sem,5,'|');
file.getline(s[i].branch,5,'!');
file.getline(extra,45,'\n');
i++;
}
i--;
for(j=0;j<i;j++)
{
if(strcmp(usn,s[j].usn)==0)
{
cout<<"\nthe old values of the record with usn"<<usn<<"are";
cout<<"\n\nNAME\t\tUSN\t\tAGE\t\tSEM\t\tBRANCH\n";
cout<<"----\t\t---\t\t---\t\t---\t\t------\n";
cout<<s[j].name<<"\t"<<s[j].usn<<"\t"<<s[j].age<<"\t"<<s[j].sem<<"\t"<<s[j].branch<<"\n";
cout<<"\n\nenter the new values\n";
cout<<"\nname = ";
cin>>s[j].name;
cout<<"\nusn = ";
cin>>s[j].usn;
cout<<"\nage = ";
cin>>s[j].age;
cout<<"\nsem = ";
cin>>s[j].sem;
cout<<"\nbranch = ";
cin>>s[j].branch;
break;
}
}
if(j==i)
{
cout<<"\nthe record with usn "<<usn<<"is not present ";
return;
}
file.close();
file.open("p2.txt",ios::out);
if(!file)
{
cout<<"\nunable to open the file in output mode";
return;
}
for(j=0;j<i;j++)
{
strcpy(buffer,s[j].name);
strcat(buffer,"|");
strcat(buffer,s[j].usn);
strcat(buffer,"|");
strcat(buffer,s[j].age);
strcat(buffer,"|");
strcat(buffer,s[j].sem);
strcat(buffer,"|");
strcat(buffer,s[j].branch);
int count=strlen(buffer);
for(int k=0;k<45-count;k++)
strcat(buffer,"!");
strcat(buffer,"\n");
file<<buffer;
}
file.close();
}
int main()
{
int choice;
while(1)
{
cout<<"\n 0 : exit \n 1 : write to file\n 2 : display the file\n 3 : modify the file\n 4 : search \n\n enter the choice : ";
cin>>choice;
switch(choice)
{
case 1: writerecord();
break;
case 2: displayFile();
break;
case 3: modify();
break;
case 4: search();
break;
case 0: exit(0);
default:cout<<"\ninvalid input...";
break;
}
}
return 0;
}
|