New Member FAQ | Forums | Earn Revenue


Resources Entrance Ask Experts Exam Papers Jobs English Projects Universities Colleges Courses Schools Training My India



My Profile
Active Members
TodayLast 7 Days more...



Awards & Gifts
Online Exams

Fresher Jobs


Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian cities including Bangalore, Chennai, Hyderabad, Pune or Kochi

Resources


Find educational articles, blogs, discussion threads and other resources.

Colleges


Find details about any college in India or search for courses.

website counter



To search a file n update the item using c++


Posted Date:     Total Responses: 0    Posted By: shafreen zeba   Member Level: Silver   Points/Cash: 3   


#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




Project Feedbacks


No feedbacks found. Be the first to respond and make money from revenue sharing program.

Post Feedback
You must Sign In to post a feedback.
Next Project: To calculate the total marks
Previous Project: 2d geometry using Turbo C Graphics

Return to Project Index

Post New Project


Related Projects






Contact Us   Advertise   Editors    Privacy Policy    Terms Of Use   

AdSense Revenue Sharing sites

ISC Technologies.
2006 - 2009 All Rights Reserved.