File reading and position question
Find the output of the following C++ code considering that the
binary file CLIENTS.DAT exists on the hard disk with records of 100
members.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
class CLIENTS
{
int Cno;char Name[20];
public :
void In(); void Out();
};
void main{)
{
fstream CF;
CF.open("CLIENTS.DAT",ios:: binary| ios::in) ;
CLIENTS C;
CF.read((char*)&C,sizeof(C));
CF.read((char*)&C,sizeof(C));
CF.read((char*)&C,sizeof(C));
int POS=CF.tellg()/sizeof(C);
cout<<"PRESENT RECORD:"<<POS<<endl;
CF.close() ;
}
|
My attempt:
CF.tellg()/sizeof(C) tells current position of file pointer. so answer is 300
but answer is 3. how?
CF.tellg() tells you the current position in bytes. Dividing by the size of C gives you the number of records that have been read.
Topic archived. No new replies allowed.