hello all it is my first time here to post :cheesy:
well i hope i'm welcomed here
my problem is i want to read binary file and view it on the console two times
#include<iostream>
#include<fstream>
using namespace std;
class Student
{
private:
char name[20];
char address[80];
int age;
public:
Student(char n[]="",char a[]="",int x=0):age(x {
strcpy(name,n);
strcpy(address,a);
}
~Student(){}
void set()
{
cin.ignore(5,'\n');
cout<<"Enter Student name ? ";
cin.getline(name,20,'\n');
if(cin.gcount()>=20)
cin.ignore(cin.gcount()-20,'\n');
cout<<"Enter Student Address ? ";
cin.getline(address,80,'\n');
if(cin.gcount()>=80)
cin.ignore(cin.gcount()-80,'\n');
cout<<"Enter Student Age ? ";
cin>>age;
}
void show()
{
for(int i=0;i<80;cout<<'\\',i++);
cout<<"The Student's Name is "<<name<<endl;
cout<<"The Student's Address is "<<address<<endl;
cout<<"The Student's Age is "<<age<<endl;
}
};
int main()
{
Student st;
fstream File("ttt.txt",ios::in|ios::out|ios::binary);
cout<<"First Adding "<<endl;
st.set();
File.write((char*)&st,sizeof(Student));
cout<<"Second adding "<<endl;
st.set();
File.write((char*)&st,sizeof(Student));
while(!File.eof())
{
st.show();
File.read((char*)&st,sizeof(Student));
}
File.seekg(0,ios::beg);
cout<<File.tellg()<<endl;
//tellg()=-1???
File.read((char*)&st,sizeof(Student));
while(!File.eof())
{
st.show();
File.read((char*)&st,sizeof(Student));
}
return 0;
}
when i run my program after adding two student objects
it views the file only one time :( and it print that File.tellg()=-1
why is it ?although i wrote File.seekg(0,ios::beg) :sad:
if there is any syntax problem fix it because i wrote this file here without vc++
and thanks in advance for helping me :surprised