#include #include #include #include #include
class inventory //class specification { char name[10]; int code; float cost; public: //public members void getdata(void) { cout<<"\nenter name:\n"; cin>>name; cout<<"\nenter code:\n"; cin>>code; cout<<"\nenter cost:\n"; cin>>cost; } void putdata(void) { cout<} };
void main() { clrscr(); inventory item; //creation of object fstream inoutfile; //creation of filestream object inoutfile.open("STOCK dat",ios::ate|ios::in|ios::out|ios::binary); //member function open with file modes inoutfile.seekg(0,ios::beg); //file pointers cout<<"\ncurrent content of stack\n"; while(inoutfile.read((char*)&item,sizeof(item))) //reading of data { item.putdata(); //accessing member function } inoutfile.clear(); //closing the file
/* add one more item */ cout<<"\nadd an item\n\n"; item.getdata(); char ch; cin.get(ch); //unformatted i/o operation inoutfile.write((char *)&item,sizeof(item));
/* display the appended file */ inoutfile.seekg(0); cout<<"\ncontents of appended file\n"; while(inoutfile.read((char *)&item,sizeof(item))) { item.putdata(); }
/*find no. of objects in file*/ int last=inoutfile.tellg(); int n=last/sizeof(item); cout<<"\nnumber of objetcs="< /*modify the details of an item*/ cout<<"total bytes in the file="<cout<<"\n enter object no. to be updated\n\n" ; int object; cin>>object; cin.get(ch); int location=(object-1)*sizeof(item); if(inoutfile.eof()) inoutfile.clear(); inoutfile.seekp(location); cout<<"enter new value of the object\n"; item.getdata(); cin.get(ch); inoutfile.write((char *)&item,sizeof(item))< /*show updated file*/ inoutfile.seekg(0); cout<<"\ncontents of updated file\n"; while(inoutfile.read((char *)&item,sizeof(item))) { item.putdata(); } inoutfile.close(); getch(); } Output : Current contents of the stack : Suresh 80 1000 Add an item : Enter the name : Dwarakh Enter the code : 83 Enter the cost : 1900 Contents of the stack : Suresh 80 1000 Dwarakh 83 1900 Number of objects = 2 Number of bytes o the file = 32 Enter the object to be updated : 1 To update an item : Enter the name : Vijay Enter the code : 86 Enter the cost : 1950 Contents of the updated file : Vijay 86 1950 Dwarakh 83 1900
|
No feedbacks found. Be the first to respond and make money from revenue sharing program.
|