Jul 29, 2012 at 12:55pm UTC
i want to create an inventory system using c++. I want to delete an item from my stored items. But I am unable to do this. And there is slight problem in the output format. Any kind of suggestion and idea would be appreciated.
thank you. Here is the program:
#include<iostream>
#include<fstream>
#include<iomanip>
#include<conio.h>
using namespace std;
class inventory
{
char name[10]; // item name
int code; //item code
long double cost;//cost of each item
public:
void readdata (void);
void writedata (void);
};
void inventory :: readdata(void) //read from keyboard
{
cout<<"enter name:";
cin>>name;
cout<<"enter code:";
cin>>code;
cout<<"enter cost:";
cin>>cost;
}
void inventory :: writedata(void) //formatted diplay on screen
{
cout<<setiosflags(ios :: left)
<<setw(10)<<name
<<setiosflags(ios :: right)
<<setw(10)<<code
<<setprecision(2)
<<setw(10)<<cost
<<endl;
}
int main ()
{
inventory item[3];// declare array of 3 object
fstream file; //input and output file;
file.open("stock.DAT",ios::in | ios::out);
cout<<"enter detail for three items\n";
for (int i=0;i<3;i++)
{
item[i].readdata();
file.write((char *) & item[i],sizeof(item[i]));
}
file.seekg(0);
cout<<"\noutput\n\n";
for (int i=0;i<3;i++)
{
file.read((char *) & item[i],sizeof(item[i]));
item[i].writedata();
}
file.close();
getch();
return 0;
}
Last edited on Jul 29, 2012 at 1:04pm UTC
Jul 29, 2012 at 1:21pm UTC
Are you asking how to remove some words from a file (stock.DAT)?
Jul 29, 2012 at 1:34pm UTC
yes . Example: i want to delete the object 2 and show the output in serial i.e item 1 and 3
Jul 29, 2012 at 2:25pm UTC
You cannot delete bits of a file. You must copy the whole file, except the bits you don't want, and then delete the original file and rename the copy.
Jul 30, 2012 at 9:16am UTC
Thanks but when I want to get output from the file , it is giving garbage value. Can you give me any solution for that?
Jul 30, 2012 at 9:20am UTC
Open file in editor. Check the contents. If contents are garbage, don't write garbage to file. If contents are not garbage, fix reading code.