Google PlusFacebookTwitter

Binary Files – Book Database

By on Sep 6, 2007 in On A Whim | 0 comments

/* Copyright (C) 2007 Ankur Banerjee. This program is free software: you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. For a copy of the GNU General Public License see http://www.gnu.org/licenses/gpl.html */ /* Binary Files – Book Database Management. Give user option to create, append, read, edit, insert, delete, search, and count total number of records. */ #include <fstream.h> #include <conio.h> #include <stdio.h> #include <string.h> class book { int bookid, noofpages; char name[21]; public: void getdata() { cout<<endl<<“Enter book ID : “; cin>>bookid; cout<<“Enter book name : “; gets(name); cout<<“Enter number of pages in book : “; cin>>noofpages; } void disp() { cout<<endl<<“Displaying data for book” <<endl<<“Book ID : “<<bookid <<endl<<“Book name : “<<name <<“\nNumber of pages : “<<noofpages<<endl; } char* retname() { return name; } int retid() { return bookid; } } obj1, obj2; char ans; fstream file, temp; int ch, flag, r, rec; void create() { file.open(“data”, ios::out | ios::binary); do { obj1.getdata(); file.write((char*)&obj1, sizeof(obj1)); cout<<“Do you wish to enter another record? (y/n): “; cin>>ans; } while (ans == ‘y’ || ans == ‘Y’); file.close(); } void append() { file.open(“data”, ios::app); do { obj1.getdata(); file.write((char*)&obj1, sizeof(obj1)); cout<<“Do you wish to enter another record? (y/n): “; cin>>ans; } while (ans == ‘y’ || ans == ‘Y’); file.close(); } void showfile() { file.open(“data”, ios::in); while (file.read((char*)&obj1, sizeof(obj1))) obj1.disp(); file.close(); } void rewrite() { temp.open(“new”, ios::in); file.open(“data”, ios::out); temp.seekg(0); while (temp.read((char*)&obj2, sizeof(obj2))) file.write((char*)&obj2, sizeof(obj2)); file.close(); temp.close(); remove(“new”); } void editrec() { file.open(“data”, ios::in); temp.open(“new”, ios::out); r = 0; cout<<endl<<“Enter record number to edit : “; cin>>rec; file.seekg(0); while (file.read((char*)&obj2, sizeof(obj2))) { r++; if (r == rec) { cout<<endl<<“Enter new details”; obj1.getdata(); temp.write((char*)&obj1, sizeof(obj1)); } else temp.write((char*)&obj2, sizeof(obj2)); } file.close(); temp.close(); rewrite(); cout<<“\nRecord you wanted to edit has been edited\n”; } void delrec() { file.open(“data”, ios::in); temp.open(“new”, ios::out); r = 1; cout<<endl<<“Enter record number to delete : “; cin>>rec; file.seekg(0); while (file.read((char*)&obj2, sizeof(obj2))) { if (r != rec) temp.write((char*)&obj2, sizeof(obj2)); ++r; } file.close(); temp.close(); rewrite(); cout<<“\nRecord you requested to be removed has been deleted\n”; } void insrec() { file.open(“data”, ios::in); temp.open(“new”, ios::out); r = 0; cout<<endl<<“Enter position at which you want to insert new record : “; cin>>rec; file.seekg(0); while (file.read((char*)&obj2, sizeof(obj2))) { r++; if (r == rec) { cout<<endl<<“Enter details of new record :”; obj1.getdata(); temp.write((char*)&obj1, sizeof(obj1)); temp.write((char*)&obj2, sizeof(obj2)); } else temp.write((char*)&obj2, sizeof(obj2)); } file.close(); temp.close(); rewrite(); cout<<“\nRecord has been inserted\n”; } void insafterid() { file.open(“data”, ios::in); temp.open(“new”, ios::out); cout<<“\nEnter book ID after which you want to insert new record : “; cin>>rec; file.seekg(0); while (file.read((char*)&obj2, sizeof(obj2))) { if (obj2.retid() != rec) temp.write((char*)&obj2, sizeof(obj2)); else { cout<<endl<<“Enter details for new record :”; obj1.getdata(); temp.write((char*)&obj2, sizeof(obj2)); temp.write((char*)&obj1, sizeof(obj1)); } } file.close(); temp.close(); rewrite(); cout<<“\nRecord has been inserted\n”; } void search() { file.open(“data”, ios::in); cout<<endl<<“Search according to” <<endl<<“\t1. ID” <<endl<<“\t2. Name” <<endl<<“Enter your choice : “; cin>>ch; switch(ch) { case 1 : int id; flag = 0; cout<<endl<<“Enter ID to search for : “; cin>>id; while (file.read((char*)&obj1, sizeof(obj1))) { if (obj1.retid() == id) { obj1.disp(); flag = 1; } } file.close(); if (flag == 0) cout<<“\nID you entered does not exist\n”; break; case 2 : char nm[21]; flag = 0; cout<<endl<<“Enter name to search for : “; gets(nm); while (file.read((char*)&obj1, sizeof(obj1))) { if (strcmp(obj1.retname(),nm) == 0) { obj1.disp(); flag = 1; } } file.close(); if (flag == 0) cout<<“\nName you entered does not exist\n”; break; default : cout<<endl<<“You entered an incorrect choice”<<endl; } } void countrec() { r = 0; file.open(“data”, ios::in); while (file.read((char*)&obj1, sizeof(obj1))) ++r; file.close(); cout<<endl<<“Number of records is “<<r<<endl; } void main() { clrscr(); do { cout<<endl<<“Book Data Management Program” <<endl<<“1. Create file” <<endl<<“2. Append file” <<endl<<“3. Read file” <<endl<<“4. Edit nth record” <<endl<<“5. Delete nth record” <<endl<<“6. Insert record at nth position” <<endl<<“7. Insert record after a book ID” <<endl<<“8. Search for a book” <<endl<<“9. Count total number of records” <<endl<<“Enter your choice : “; cin>>ch; switch(ch) { case 1 : create(); break; case 2 : append(); break; case 3 : showfile(); break; case 4 : editrec(); break; case 5 : delrec(); break; case 6 : insrec(); break; case 7 : insafterid(); break; case 8 : search(); break; case 9 : countrec(); break; default : cout<<endl<<“You entered an incorrect choice”; } cout<<“\nDo you want the menu to be shown again? (y/n): “; cin>>ans; } while (ans == ‘y’ || ans == ‘Y’ ); getch(); } /* Output */ Book Data Management Program 1. Create file 2. Append file 3. Read file 4. Edit nth record 5. Delete nth record 6. Insert record at nth position 7. Insert record after a book ID 8. Search for a book 9. Count total number of records Enter your choice : 1 Enter book ID : 1 Enter book name : PhysicsEnter number of pages in book : 145 Do you wish to enter another record? (y / n): y Enter book ID : 2 Enter book name : Chemistry Enter number of pages in book : 214 Do you wish to enter another record? (y / n): n Do you want the menu to be shown again? (y / n) : y...

Binary Files – Employee Data

By on Sep 5, 2007 in On A Whim | 0 comments

/* Copyright (C) 2007 Ankur Banerjee. This program is free software: you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. For a copy of the GNU General Public License see http://www.gnu.org/licenses/gpl.html */ /* Binary Files – Employee Data Management Program. Give user the option to create & delete file, show number of records, delete particular record, and delete last record */ #include <fstream.h>#include <conio.h>#include <stdio.h> class employee{int eno;char ename[20];double salary;public: void getdata(){cout<<endl<<“Enter employee number : “;cin>>eno;cout<<“Enter employee name : “;gets(ename);cout<<“Enter employee salary : “;cin>>salary;}void disp(){cout<<endl<<Displaying data for employee”<<endl<<“Employee number : “<<eno<<endl<<“Employee name : “<<ename<<endl<<“Employee salary : “<<salary<<endl;}} obj1, obj2; char ans;fstream file, temp;int ch, r; void create(){ file.open(“data”, ios::out | ios::binary);do{obj1.getdata();file.write((char*)&obj1, sizeof(obj1));cout<<“Do you wish to enter another record? (y / n): “;cin>>ans;} while (ans == ‘y’ || ans == ‘Y’);file.close();} void showfile(){file.open(“data”, ios::in);while (file.read((char*)&obj1, sizeof(obj1)))obj1.disp();file.close();} void countrec(){r = 0; file.open(“data”, ios::in);while (file.read((char*)&obj1, sizeof(obj1)))++r;file.close();cout<<endl<<“Number of records is “<<r<<endl;} void rewrite(){temp.open(“new”, ios::in);file.open(“data”, ios::out);temp.seekg(0);while (temp.read((char*)&obj2, sizeof(obj2)))file.write((char*)&obj2, sizeof(obj2));file.close();temp.close();remove(“new”);cout<<“\nRecord you requested to be removed has been deleted\n”;} void delrec(){file.open(“data”, ios::in);temp.open(“new”, ios::out);r = 1;int rec;cout<<endl<<“Enter record number to delete : “;cin>>rec; file.seekg(0);while (file.read((char*)&obj2, sizeof(obj2))){if (r != rec)temp.write((char*)&obj2, sizeof(obj2)); ++r;}file.close();temp.close();rewrite();} void dellast(){file.open(“data”, ios::in);temp.open(“new”, ios::out);file.seekg(0, ios::end); // go to end of fileint lp = file.tellg(); int s = sizeof(obj1);int lr = lp / s;r = 1;file.seekg(0);while (file.read((char*)&obj2, sizeof(obj2))){if (r != lr)temp.write((char*)&ob j2, sizeof(obj2));++r;}file.close();temp.close();rewrite();} void main(){clrscr();do{cout<<endl<<“Employee Data Management Program” <<endl<<“1. Create file”<<endl<<“2. Read file”<<endl<<“3. Find total number of records”<<endl<<“4. Delete nth record”<<endl<<“5. Delete the last record”<<endl<<“Enter your choice : “;cin>>ch;switch(ch){case 1 : create();break;case 2 : showfile();break;case 3 : countrec();break;case 4 : delrec();break;case 5 : dellast();break;default : cout<<“\nYou entered an incorrect choice”;}cout<<“\nDo you want the menu to be shown again? (y / n): “;cin>>ans;} while (ans == ‘y’ || ans == ‘Y’ );getch();} /* Output */ Employee Data Management Program1. Create file2. Read file3. Find total number of records4. Delete nth record5. Delete the last recordEnter your choice : 1 Enter employee number : 1Enter employee name : Arthur DentEnter employee salary : 45000Do you wish to enter another record? (y / n): y Enter employee number : 2Enter employee name : Tricia McMillianEnter employee salary : 12000Do you wish to enter another record? (y / n): y Enter employee number : 3Enter employee name : Ford PrefectEnter employee salary : 50000Do you wish to enter another record? (y / n): y Enter employee number : 4Enter employee name : Zaphod BeeblebroxEnter employee salary : 30000Do you wish to enter another record? (y / n): y Enter employee number : 5Enter employee name : ZarniwoopEnter employee salary : 6000Do you wish to enter another record? (y / n): n Do you want the menu to be shown again? (y / n) : y Employee Data Management Program 1. Create file 2. Read file 3. Find total number of records 4. Delete nth record 5. Delete the last record Enter your choice : 3 Number of records is 5 Do you want the menu to be shown again? (y / n) : y Employee Data Management Program 1. Create file 2. Read file 3. Find total number of records 4. Delete nth record 5. Delete the last record Enter your choice : 5 Record you requested to be removed has been deleted Do you want the menu to be shown again? (y / n) : y Employee Data Management Program 1. Create file 2. Read file 3. Find total number of records 4. Delete nth record 5. Delete the last record Enter your choice : 2 Displaying data for employeeEmployee number : 1Employee name : Arthur DentEmployee salary : 45000 Displaying data for employeeEmployee number : 2Employee name : Tricia McMillianEmployee salary : 12000 Displaying data for employeeEmployee number : 3Employee name : Ford PrefectEmployee salary : 50000 Displaying data for employeeEmployee number : 4Employee name : Zaphod BeeblebroxEmployee salary : 30000 Do you want the menu to be shown again? (y / n) : y Employee Data Management Program 1. Create file 2. Read file 3. Find total number of records 4. Delete nth record 5. Delete the last record Enter your choice : 4 Enter record number to delete : 2 Record you requested to be removed has been deleted Do you want the menu to be shown again? (y / n) : y Employee Data Management Program 1. Create file 2. Read file 3. Find total number of records 4. Delete nth record 5. Delete the last record Enter your choice : 2 Displaying data for employee Employee number : 1 Employee name : Arthur Dent Employee salary : 45000 Displaying data for employee Employee number : 3 Employee name : Ford Prefect Employee salary : 50000 Displaying data for employee Employee number : 4 Employee name : Zaphod Beeblebrox Employee salary : 30000 Do you want the menu to be shown again? (y / n) : n /* Program faults: It does not check whether record number to be deleted exceeds available number of records or not. Can be easily checked by taking value from countrec() and putting a check in delrec()...

Binary Files – Floppy Box

By on Sep 5, 2007 in On A Whim | 0 comments

/* Copyright (C) 2007 Ankur Banerjee. This program is free software: you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. For a copy of the GNU General Public License see http://www.gnu.org/licenses/gpl.html */ /* Binary Files – Floppy Box Management Program */ #include <fstream.h> #include <conio.h> #include <stdio.h> #include <string.h> class floppybox { int floppyid, size; char name[42]; public: void getdata() { cout<<endl<<“Enter floppy ID : “; cin>>floppyid; cout<<“Enter floppy name : “; gets(name); cout<<“Enter floppy size : “; cin>>size; } void disp() { cout<<“\n\nDisplaying data for floppy” <<endl<<“Floppy ID : “<<floppyid <<endl<<“Floppy name : “<<name <<endl<<“Floppy size : “<<size<<endl; } char* retname() { return name; } int retid() { return floppyid; } } obj1; char ans; fstream file; int ch, flag; void create() { file.open(“data”, ios::out | ios::binary); do { obj1.getdata(); file.write((char*)&obj1, sizeof(obj1)); cout<<“Do you wish to enter another record? (y / n): “; cin>>ans; } while (ans == ‘y’ || ans == ‘Y’); file.close(); } void showfile() { file.open(“data”, ios::in); while (file.read((char*)&obj1, sizeof(obj1))) obj1.disp(); file.close(); } void countrec() { int r = 0; file.open(“data”, ios::in); while (file.read((char*)&obj1, sizeof(obj1))) ++r; file.close(); cout<<endl<<“Number of records is “<<r<<endl; } void search() { file.open(“data”, ios::in); cout<<endl<<“Search according to” <<endl<<“\t1. ID” <<endl<<“\t2. Name” <<endl<<“Enter your choice : “; cin>>ch; switch(ch) { case 1 : int id; flag = 0; cout<<endl<<“Enter ID to search for : “; cin>>id; while (file.read((char*)&obj1, sizeof(obj1))) { if (obj1.retid() == id) { obj1.disp(); flag = 1; } } file.close(); if (flag == 0) cout<<“\nID you entered does not exist\n”; break; case 2 : char nm[42]; flag = 0; cout<<endl<<“Enter name to search for : “; gets(nm); while (file.read((char*)&obj1, sizeof(obj1))) { if (strcmp(obj1.retname(),nm) == 0) { obj1.disp(); flag = 1; } } file.close(); if (flag == 0) cout<<“\nName you entered does not exist\n”; break; default : cout<<“\nYou entered an incorrect choice\n”; } } void main() { clrscr(); do { cout<<endl<<“Floppy Box Management Program” <<endl<<“1. Create file” <<endl<<“2. Read file” <<endl<<“3. Find total number of records” <<endl<<“4. Search and display floppy details” <<endl<<“Enter your choice : “; cin>>ch; switch(ch) { case 1 : create(); break; case 2 : showfile(); break; case 3 : countrec(); break; case 4 : search(); break; default : cout<<“\nYou entered an incorrect choice”; } cout<<“\nDo you want the menu to be shown again? (y/n): “; cin>>ans; } while (ans == ‘y’ || ans == ‘Y’ ); getch(); } /* Output */ Floppy Box Management Program 1. Create file 2. Read file 3. Find total number of records 4. Search and display floppy details Enter your choice : 1 Enter floppy ID : 1 Enter floppy name : Samsung Enter floppy size : 4 Do you wish to enter another record? (y / n): y Enter floppy ID : 2 Enter floppy name : Sony Enter floppy size : 3 Do you wish to enter another record? (y / n): y Enter floppy ID : 3 Enter floppy name : IBM Enter floppy size : 2 Do you wish to enter another record? (y / n): n Do you want the menu to be shown again? (y / n) : y Floppy Box Management Program 1. Create file 2. Read file 3. Find total number of records 4. Search and display floppy details Enter your choice : 2 Displaying data for floppy Floppy ID : 1 Floppy name : Samsung Floppy size : 4 Displaying data for floppy Floppy ID : 2 Floppy name : Sony Floppy size : 3 Displaying data for floppy Floppy ID : 3 Floppy name : IBM Floppy size : 2 Do you want the menu to be shown again? (y / n) : y Floppy Box Management Program 1. Create file 2. Read file 3. Find total number of records 4. Search and display floppy details Enter your choice : 3 Number of records is 3 Do you want the menu to be shown again? (y / n) : y Floppy Box Management Program 1. Create file 2. Read file 3. Find total number of records 4. Search and display floppy details Enter your choice : 4 Search according to 1. ID 2. Name Enter your choice : 1 Enter ID to search for : 1 Displaying data for floppy Floppy ID : 1 Floppy name : Samsung Floppy size : 4 Do you want the menu to be shown again? (y / n) : y Floppy Box Management Program 1. Create file 2. Read file 3. Find total number of records 4. Search and display floppy details Enter your choice : 4 Search according to 1. ID 2. Name Enter your choice : 2 Enter name to search for : IBM Displaying data for floppy Floppy ID : 3 Floppy name : IBM Floppy size : 2 Do you want the menu to be shown again? (y / n) :...

Text File – No Uppercase

By on Sep 5, 2007 in On A Whim |

/* Copyright (C) 2007 Ankur Banerjee. This program is free software: you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. For a copy of the GNU General Public License see http://www.gnu.org/licenses/gpl.html */ /* Text Files – Convert file contents to uppercase and copy to another file */ #include <fstream.h> #include <stdio.h> #include <ctype.h> #include <conio.h> void main() { clrscr(); fstream obj(“text.txt”, ios::out); char s[101], ans, ch; do { cout<<endl<<“Enter data (max 100 characters) : “; gets(s); obj<<s<<‘\n’; cout<<“Do you wish to continue (y / n) : “; cin>>ans; } while (ans == ‘y’); obj.close(); obj.open(“text.txt”, ios::in); fstream obj2(“new.txt”, ios::out); while ( !obj.eof() ) { ch = obj.get(); ch = (char)toupper(ch); // type-casting while conversion obj2<<ch; } obj.close(); obj2.close(); cout<<endl<<“New file is”<<endl; obj2.open(“new.txt”, ios::in); while ( !obj2.eof() ) { ch = obj2.get(); cout<<ch; }; obj2.close(); getch(); } /* Output */ Enter data (max 100 characters) : The Answer to Life, the Universe, and Everything is 42 Do you wish to continue (y / n) : n New file is THE ANSWER TO LIFE, THE UNIVERSE, AND EVERYTHING IS...

Text Files – No Words Starting With Vowels

By on Sep 5, 2007 in On A Whim | 0 comments

/* Copyright (C) 2007 Ankur Banerjee. This program is free software: you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. For a copy of the GNU General Public License see http://www.gnu.org/licenses/gpl.html */ /* Text Files – Copy those words which do not start with a vowel to a new file from an existing file */ #include <fstream.h> #include <stdio.h> #include <conio.h> void main() { clrscr(); fstream obj(“text1.txt”, ios::out); char s[101], ans, ch; do { cout<<endl<<“Enter data (max 100 characters) : “; gets(s); obj<<s<<‘\n’; cout<<“Do you wish to continue (y / n) : “; cin>>ans; } while (ans == ‘y’); obj.close(); obj.open(“text1.txt”, ios::in); fstream obj2(“text2.txt”, ios::out); while ( !obj.eof() ) { obj.getline(s, 101, ‘ ‘); switch (s[0]) { case ‘A’ : case ‘a’ : case ‘E’ : case ‘e’ : case ‘I’ : case ‘i’ : case ‘O’ : case ‘o’ : case ‘U’ : case ‘u’ : break; default : obj2<<s<<‘\n’; } } obj.close(); obj2.close(); cout<<endl<<“New file is :”<<endl; obj2.open(“text2.txt”, ios::in); while ( !obj2.eof() ) { ch = obj2.get(); cout<<ch; }; obj2.close(); getch(); } /* Output */ Enter data (max 100 characters) : The Answer to Life, the Universe, and Everything is 42 Do you wish to continue (y / n) : n New file is : The to Life, the...