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:

using namespace std;
class inventory


char name[10]; // item name
int code; //item code
long double cost;//cost of each item
void readdata (void);
void writedata (void);

void inventory :: readdata(void) //read from keyboard
cout<<"enter name:";
cout<<"enter code:";
cout<<"enter cost:";
void inventory :: writedata(void) //formatted diplay on screen
cout<<setiosflags(ios :: left)
<<setiosflags(ios :: right)


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++)
file.write((char *) & item[i],sizeof(item[i]));


for (int i=0;i<3;i++)
file.read((char *) & item[i],sizeof(item[i]));
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.