#2
| |||
| |||
![]()
As you Asking for the File Structure lab Program of VTU University the Programes Are given below File Structure Lab Programs – Part A PROGRAM-1 #include #include #include void swap (char &a,char &b) { char temp; temp=a; a=b; b=temp; } void reverse(char *name) { int i,j,size; size=strlen(name); for(i=0,j=size-1;i } void main(int argc,char *argv[]) { char name[30]; if(argc==1) { do { cout<<"enter name\n"; cin>>name; reverse(name); cout< while(!cin.eof()); } else if(argc==3) { fstream ip,op; ip.open(argv[1],ios::in); op.open(argv[2],ios: ![]() do { ip>>name; reverse(name); cout< break; } while(1); } else cout<<"error"; } // ctrl-c to exit !!! Execution of file -> 1.CPP 1.start ->type cmd . 2. In commad Prompt type -> d :-> cd debug -> 3.Create 2 files 1.txt(Enter some names ) and 2.txt (keep it empty ) 4. After executing program there will be a file called 1.exe in debug folder. 5. Type in 1.exe 1.txt 2.txt ( Reversed names will be found in 2.txt ) PROGRAM-2 Write a C++ program to read and write student objects with fixedlength records and the fields delimited by “|”. Implement pack ( ), unpack ( ), modify ( ) and search ( ) methods. #include #include #include #include class FLB; class person { char usn[30],name[30],address[30],branch[30],college[30]; public: void input(); void output(); void search(char *fname); void modify(char *fname); friend class FLB; }; class FLB { char buff[160]; public: FLB() { for(int i=0;i<160;i++) buff[i]='\0'; } void pack(person &p); void unpack(person &p); void read(fstream &fs); void write(char *fname); }; void person::input() { cout<<"enter usn,name,address,branch and college\n"; cin>>usn>>name>>address>>branch>>college; } void person: ![]() { cout<<"usn:"< void FLB: ![]() { strcpy(buff,p.usn);strcat(buff,"|"); strcat(buff,p.name);strcat(buff,"|"); strcat(buff,p.address);strcat(buff,"|"); strcat(buff,p.branch);strcat(buff,"|"); strcat(buff,p.college); } void FLB::unpack(person &p) { char *ptr=buff; while(*ptr) { if(*ptr=='|') *ptr='\0'; ptr++; } ptr=buff; strcpy(p.usn,ptr); ptr+=strlen(ptr)+1; strcpy(p.name,ptr); ptr+=strlen(ptr)+1; strcpy(p.address,ptr); ptr+=strlen(ptr)+1; strcpy(p.branch,ptr); ptr+=strlen(ptr)+1; strcpy(p.college,ptr); } void FLB::read(fstream &fs) { fs.read(buff,sizeof(buff)); } void FLB::write(char *fname) { fstream os(fname,ios::app); os.write(buff,sizeof(buff)); os.close(); } void person::search(char *fname) { int found=0; person p; FLB b; char key[30]; fstream is(fname,ios::in); cout<<"enter the usn to be searched"; cin>>key; while((!is.eof())&&(!found)) { b.read(is); if(is.eof()) break; b.unpack(p); if(strcmp(p.usn,key)==0) { cout<<"record found\n"; p.output(); found=1; } } if(!found) cout<<"does not exists\n"; is.close(); } void person::modify(char *fname) { int found=0; person p; FLB b; char key[30],tname[]="temp.txt"; fstream is(fname,ios::in); fstream tfile(tname,ios: ![]() cout<<"enter the usn to be searched"; cin>>key; while((!is.eof())) { b.read(is); if(is.eof()) break; b.unpack(p); if((strcmp(p.usn,key)==0)&&(!found)) { cout<<"record found\n"; p.input(); found=1; } b.pack(p); b.write(tname); } if(!found) cout<<"does not exists\n"; is.close(); tfile.close(); //remove(fname); //rename(tname,fname); } void main() { person p; FLB b; int ch; char fname[]="prg2.txt"; do { cout<<"1.insert\t 2.search\t 3.modify\t 4.exit\n enter ur choice\n"; cin>>ch; switch(ch) { case 1: p.input(); b.pack(p); b.write(fname); break; case 2: p.search(fname); break; case 3: p.modify(fname); break; case 4: //remove(fname); break; } }while(ch!=4); PROGRAM -3 #include #include #include int n, no_of_rec, length, len[20]; class student { public: char usn[20], sem[20], name[20], branch[20]; }; class textbuff { public: char buff[500]; void pack(student s[]) { fstream file1("first.txt", ios: ![]() clear_buff(); for(int i = 0; i < n; i++) { strcat(buff, s[i].usn); strcat(buff, "|"); strcat(buff, s[i].name); strcat(buff, "|"); strcat(buff, s[i].branch); strcat(buff, "|"); strcat(buff, s[i].sem); strcat(buff, "|"); no_of_rec++; len[no_of_rec] = strlen(buff) + length; } length = strlen(buff); file1< } void unpack(student s[]) { fstream file2("first.txt", ios::in); clear_buff(); for(int i = 0; i < no_of_rec; i++) { file2.getline(s[i].usn, 20, '|'); file2.getline(s[i].name, 20, '|'); file2.getline(s[i].branch, 20, '|'); file2.getline(s[i].sem, 20, '|'); } } void clear_buff() { for(int i = 0; i < 500; i++) buff[i] = NULL; } void display(student s[]) { char ch; if(no_of_rec != 0) { cout<<"USN\tName\tBranch\tSem\n"; for(int i = 0; i < no_of_rec; i++) { ch = s[i].usn[0]; if(ch != '*') cout< } else cout<<"No record found\n"; } void search(student s[]) { int flag = 0; char key[20]; clear_buff(); cout<<"Enter the USN for search: "; cin>>key; for(int i = 0; i < no_of_rec; i++) { if(strcmp(key, s[i].usn) == 0) { cout<<"Record found!!\n"; cout<<"USN: "< } } if(!flag) cout<<"No such USN found\n"; } void modify(student s[]) { char key[20], cvar[20]; /*int len1[20];*/ int flag = 0, len_field; /*for(int i = 1; i <= no_of_rec; i++) len1[i] = len[i] - len[i-1];*/ cout<<"Enter the USN of record to be modified: "; cin>>key; for(int i = 0; i < no_of_rec; i++) { if(strcmp(key, s[i].usn) == 0) { fstream file3("first.txt", ios::in | ios: ![]() fstream file4("first.txt", ios: ![]() file3.seekg(len[i], ios::beg); file3.getline(cvar, 20, '|'); len_field = strlen(cvar); //length of 1 field file3.seekp(len[i], ios::beg); for(int j = 0; j < len_field; j++) file3<<'*'; file3.close(); cout<<"Enter new data:\nEnter USN, name, branch and sem:\n"; cin>>s[i].usn>>s[i].name>>s[i].branch>>s[i].sem; clear_buff(); strcat(buff, s[i].usn); strcat(buff, "|"); strcat(buff, s[i].name); strcat(buff, "|"); strcat(buff, s[i].branch); strcat(buff, "|"); strcat(buff, s[i].sem); strcat(buff, "|"); no_of_rec++; len[no_of_rec] = strlen(buff) + length; // file3.seekp(0, ios::end); file4< flag = 1; } } if(!flag) cout<<"No such USN found\n"; } }; void main() { int ch, i; student s[20]; textbuff b; cout<<"1.Create\n2.Display\n3.Search\n4.Modify\n5.Exit\n"; do { cout<<"Enter your choice: "; cin>>ch; switch(ch) { case 1: cout<<"Enter no. of records: "; cin>>n; for(i = 0; i < n; i++) { cout<<"Enter USN, name, branch and sem of student "< cin>>s[i].usn>>s[i].name>>s[i].branch>>s[i].sem; } b.pack(s); break; case 2: b.unpack(s); b.display(s); break; case 3: b.unpack(s); b.search(s); break; case 4: b.unpack(s); b.modify(s); break; case 5: remove("first.txt"); break; } }while(ch != 5); } PROGRAM -4 #include #include #include int count=1; class student { public: char name[20],usn[20],branch[20],sem[20],buffer[20]; int array[20]; void input() { cout<<"enter the name\n"; cin>>name; cout<<"enter the usn\n"; cin>>usn; cout<<"enter the branch\n"; cin>>branch; cout<<"enter the sem\n"; cin>>sem; } void pack() { strcpy(buffer,name);strcat(buffer,"|"); strcat(buffer,usn);strcat(buffer,"|"); strcat(buffer,branch);strcat(buffer,"|"); strcat(buffer,sem);strcat(buffer,"|"); int length=strlen(buffer); array[count++]=length; fstream file; file.open("first.txt",ios::app); file< } void search_rrn() { int rrn,size=0; char name1[20],usn1[20],branch1[20],sem1[20]; cout<<"enter the rrn:"; cin>>rrn; if(rrn>=count) cout<<"no record found at this rrn\n"; else { for(int i=1;i fstream file; file.open("first.txt",ios::in); file.seekg(size,ios::beg); file.getline(name1,15,'|'); file.getline(usn1,15,'|'); file.getline(branch1,15,'|'); file.getline(sem1,15,'|'); cout<<"name:"< } } }; void main() { int ch; student s; do { cout<<"\n1.insert 2.search 3.exit\n"; cout<<"enter your choice\n"; cin>>ch; switch(ch) { case 1:s.input(); s.pack(); break; case 2:s.search_rrn(); break; case 3://remove("first.txt"); break; } }while(ch!=3); } PROGRAM-5 #include #include #include #include char index[100][50],indexadd[100][50]; int nor; void smi() { char temp[100],temp1[100]; for(int i=0;i for(int j=i+1;j if(strcmp(index[i],index[j])>0) { strcpy(temp,index[i]); strcmp(index[i],index[j]); strcmp(index[j],temp); strcmp(temp1,indexadd[i]); strcmp(indexadd[i],indexadd[j]); strcmp(indexadd[j],temp1); } } } } class stud { public:char usn[10],name[20],brn[10],sem[10]; void unpack(int); void add(); void del(); void search(); void disp(); }; void stud::add() { char buf[100],sadd[50]; cout<<"\n enter usn,name,branch,and sem:\n"; cin>>usn>>name>>brn>>sem; for(int i=0;i if(strcmp(usn,index[i])==0) { cout<<"\nduplicate entry\n"; return; } } strcpy(buf,usn); strcat(buf,"|"); strcat(buf,name); strcat(buf,"|"); strcat(buf,brn); strcat(buf,"|"); strcat(buf,sem); strcat(buf,"|"); fstream f1("index.txt",ios::app); fstream f2("rec.txt",ios::app); f2.seekp(0,ios::end); strcpy(index[nor],usn); itoa(f2.tellp(),sadd,10); strcpy(indexadd[nor],sadd); f1< smi(); f1.close(); f2.close(); } void stud::search() { char usn[10]; stud s; cout<<"\nenter usn for search:"; cin>>usn; int max=nor-1,min=0,flag=0,add,mid; while(min<=max&&flag==0) { mid=(min+max)/2; if(strcmp(usn,index[mid])==0) { flag=1; add=atoi(indexadd[mid]); s.unpack(add); cout<<"record found\n"; cout<<"usn:"< else if(strcmp(usn,index[mid])>0) min=mid+1; else max=mid-1; } if(!flag) cout<<"\nsearch failed!!\n"; } void stud::unpack(int add) { fstream f1("rec.txt",ios::in); f1.seekg(add); f1.getline(usn,10,'|'); f1.getline(name,10,'|'); f1.getline(brn,10,'|'); f1.getline(sem,10,'|'); f1.close(); } void stud::del() { char usn[10]; fstream f1("index.txt",ios: ![]() cout<<"\nenter usn to delete:"; cin>>usn; int max=nor-1,min=0,flag=0,mid; while(min<=max&&flag==0) { mid=(min+max)/2; if(strcmp(usn,index[mid])==0) { flag=1; cout<<"\nrecord delated\n"; for(int i=mid;i strcpy(index[i],index[i+1]); strcpy(indexadd[i],indexadd[i+1]); } nor--; } else if(strcmp(usn,index[mid])>0) min=mid+1; else max=min-1; } for(int i=0;i f1.write(index[i],strlen(index[i])); f1.write("|",1); f1.write(indexadd[i],strlen(indexadd[i])); f1.write("|\n",2); } f1.close(); if(!flag) cout<<"\ndeletion failed\n"; } void stud::disp() { stud s; int add; cout<<"usn\tname\tbranch\tsem\n"; for(int i=0;i add=atoi(indexadd[i]); s.unpack(add); cout< } void main() { stud s; int ch; cout<<"1 add\n2 search\n3 delete\n4 display\n5 exit"; do { cout<<"enter your chaice:"; cin>>ch; switch(ch) { case 1:s.add(); break; case 2:s.search(); break; case 3:s.del(); break; case 4:s.disp(); break; case 5:remove("index.txt"); remove("rec.txt"); break; } }while(ch!=5); } For Any Query you may contact to the VTU University the contact details Are given below Contact details : VTU University Address: Jnana Sangama, VTU Main Road, Machhe, Belagavi, Karnataka 590018 Phone: 0831 249 8196
__________________ Answered By StudyChaCha Member |
![]() |
|