facing problem in deleting an item in a file and the output format

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
Are you asking how to remove some words from a file (stock.DAT)?
yes . Example: i want to delete the object 2 and show the output in serial i.e item 1 and 3
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.
Thanks but when I want to get output from the file , it is giving garbage value. Can you give me any solution for that?
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.
Topic archived. No new replies allowed.