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

I suspect the fstream has gone into a failed state after the first read because you read through the entire file and encountered EOF which triggered the fail bit. Try placing this line:

File.clear();

after the while loop is done, (so you've stopped the loop because you got to the end of the file) and before this line:

File.seekg(0, ios::beg);

Also, it's not a good idea to use the return of eof() as the stop condition of a loop because it can corrupt your data.

thanks lerner for help
i took ur advise and it word fine now
but what do u suggest to use as stop condition instead of
the return of eof()
there is another problem with me
when u declare
fstream file("xxx",ios::in);
if(file)
{ cout<<"File is ok"<<endl;}
my visual c++ compiler issue error in checking for file :-|
while if i said
if(!file) it accept it
i know that there is void* operator overloading in fstream which become null in error bits :idea:
and i noticed that
ifstream file("xxx");
if(file)
{cout<<"file is ok"<<endl;}
the compiler doesn't issue any problem :!:

> what do u suggest to use as stop condition instead of
the return of eof()

Say the file consists of nine ints.

Rather than this:

while(!fin.eof())
{
fin >> num;
}

I would suggest this:

while(fin >> num)
{}

Using the return of eof() as the return value is prone to introducing error because of the technicalities associated with how eof() works. Basically, it often allows the loop to run one to many times.

Using a stream method that reads in data as the conditional will allow the loop to fail gracefully, and without "overreading the file", whatever causes the input to fail. Finding EOF is one of the conditions that would put the stream in a failed state and terminate the input, but there are other, more nefarious things that could happen as well.

As to why if(file) doesn't work all the time but if(!file) does, I don't know. Sorry. You could try if(file.good()) instead of if(file) if you want to accentuate the positive instead of the negative, I suppose.

This article has been dead for over six months. Start a new discussion instead.