Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I find the EOF?

Status
Not open for further replies.

SarahKate31

Programmer
Feb 14, 2002
34
US
Hello all...I am a beginner when it comes to C++ so hopefully someone can help! I am writing a program that should read from a .dat file into an array of structs...The first part of each line of the file will read into one variable and the second part will read into another, but I can't figure out how to find out when I've reached the end of the file since I don't know how big it is. The code I have so far seems to work except for the line determining EOF ---

int ReadData (STAR Array [])
{
static int i = 0;
static int number = 1;

static ifstream starFile ("star.dat");

if (starFile.fail())
{
cerr << &quot;An error occured in opening the file.&quot; << endl;
exit (1);
}

if (starFile.eof)
{
return i;
}

else
{
Array.Num = number;
starFile.getline (Array.itemName, 22, ',');
starFile.getline (Array.price, 7, '\n');

i ++;
number ++;

ReadData (Array);

return i;
}
}

Any insight would be much appriciated!!
 
SarahKate31 -

your are most likely looking for eof() method for the ifstream class.

what you can do is:

...
ifstream starFile (&quot;star.dat&quot;);

if(!starFile) cout<<&quot;cannot open&quot;<<endl;

while (starFile)
{
starFile.read((char *) &<your storage>, sizeof(<your storage>);
// parse <your storage>

// to prevent last record used twice
if(!starFile.eof()) {... do something}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top