I am facing problem with this c++ code in codeblocks... here is the code:
#include<iostream>
#include<fstream>
using namespace std;
class test {
int i;
float j;
public:
void getdata() {
std::cout<<"\nenter i and j:";
std::cin>>i>>j;
}
void putdata() {
std::cout<<"\ni="<<i<<"\tj="<<j;
}
};
int main()
{
test ob,ob1;
fstream f;
f.open("dil.txt",ios::out|ios::binary);
for(int i=0;i<5;i++)
{
ob.getdata();
f.write((char *)& ob,sizeof(ob));
}
f.close();
f.open("dil.txt",ios::in|ios::binary);
f.seekg(0);
while(f) {
f.read((char *)& ob1,sizeof(ob1));
ob1.putdata();
}
f.close();
return 0;
}
The output after entering the details is coming:
i=2 j=2.3
i=3 j=3.4
i=4 j=4.5
i=5 j=5.6
i=6 j=6.7
i=6 j=6.7
so,my question is why the last statement is getting repeated?...please help