Files when calling the dispaly function some garbage value is coming

A

arunvnk

int main()
{
product g;
product t;
fstream k;
k.open("stock.txt",ios::app|ios::in);
int op=0;
static int count=0;
//k.open("stock.txt",ios::app,ios::in);
while(op<4)
{
clrscr();
cout<<"\n1.Enter details";
cout<<"\n2.Modify";
cout<<"\n3.Display";
cin>>op;

if(op==1)
{
g.getdata();
k.write((char*)&g,sizeof(g));
count++;
}

if(op==2)
{
int code;
cout<<"\nEnter code of product";
cin>>code;
k.seekg(0,ios::cur);
for(int i=0;i<count;i++)
{
k.read((char*)&t,sizeof(t));
if(t.retc()== code);
{
t.getdata();
k.seekp(-sizeof(t),ios::cur);
k.write((char*)&t,sizeof(t));
}
}
}
if(op==3)
{
product h;
k.seekg(0,ios::cur);
for(int i=0;i<count;i++)
{
k.read((char*)&h,sizeof(h));
h.display();
}
}
}//while
k.close();
return(0);
}
 
R

red floyd

int main()
{
product g;
product t;
fstream k;
k.open("stock.txt",ios::app|ios::in);
int op=0;
static int count=0;
//k.open("stock.txt",ios::app,ios::in);
while(op<4)
{
clrscr();
cout<<"\n1.Enter details";
cout<<"\n2.Modify";
cout<<"\n3.Display";
cin>>op;

if(op==1)
{
g.getdata();
k.write((char*)&g,sizeof(g));
count++;
}

if(op==2)
{
int code;
cout<<"\nEnter code of product";
cin>>code;
k.seekg(0,ios::cur);
for(int i=0;i<count;i++)
{
k.read((char*)&t,sizeof(t));
if(t.retc()== code);
{
t.getdata();
k.seekp(-sizeof(t),ios::cur);
k.write((char*)&t,sizeof(t));
}
}
}
if(op==3)
{
product h;
k.seekg(0,ios::cur);
for(int i=0;i<count;i++)
{
k.read((char*)&h,sizeof(h));
h.display();
}
}
}//while
k.close();
return(0);
}

And what is your problem? What are you expecting said code to do, and
what is said code actually doing?
 
N

Nerazzurri

Maybe there are someting problem about your display function?
plz show your class implementations and the errors or warnings supplied
by the compiler
 
A

arunvnk

The program compiles perfectly.the display fuction is a normal class
funtion,which displays the members
 
S

smartbeginner

Arun,Please provide the compiler error messages.
And the operating system you are working with .
 
J

Jim Langston

int main()
{
product g;
product t;
fstream k;
k.open("stock.txt",ios::app|ios::in);
int op=0;
static int count=0;
//k.open("stock.txt",ios::app,ios::in);
while(op<4)
{
clrscr();
cout<<"\n1.Enter details";
cout<<"\n2.Modify";
cout<<"\n3.Display";
cin>>op;

if(op==1)
{
g.getdata();
k.write((char*)&g,sizeof(g));
count++;
}

if(op==2)
{
int code;
cout<<"\nEnter code of product";
cin>>code;
k.seekg(0,ios::cur);
for(int i=0;i<count;i++)
{
k.read((char*)&t,sizeof(t));
if(t.retc()== code);
{
t.getdata();
k.seekp(-sizeof(t),ios::cur);
k.write((char*)&t,sizeof(t));
}
}
}
if(op==3)
{
product h;
k.seekg(0,ios::cur);
for(int i=0;i<count;i++)
{
k.read((char*)&h,sizeof(h));
h.display();
}
}
}//while
k.close();
return(0);
}

This is not a complete compilable program. For one we can't see what
product.getdata() does, nor product.display()

Please show us the smallest compilable program that demonstrates the
problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top