On 2/8/07, Lars Finsen <[EMAIL PROTECTED]> wrote:
> Den 8. feb. 2007 kl. 15.21 skrev Paul Herring:
> >
> > It is very rare that you should be using EOF functions directly in a C
> > or C++ program - you should instead be checking for an error in the
> > reading of data from a file. Then, and only then, should you be using
> > the EOF functions to determine if indeed it is the end of the file (or
> > someone's removed the floppy disc from the drive etc.)
> Why is that? I've always used it when reading from a text file.
> Checking for other errors usually isn't necessary.
That's the Standard (TM) way. EOF will only be true after a read
operation fails, but not before. That's what causes the extra reads in
your code.
> > Show your code!
> It's very simple. Essentially it goes like this:
>
> ifstream(filename) ui;
> while(!ui.eof())
> {
> if(baner==NULL) baner = b = new bane;
> else { b->neste = new bane; b=b->neste; }
> ui >> b->fork >> b->type >>....>> b->ist;
> }
> ui.close();
Should be something like this:
ifstream ui (filename)
while(1)
{
if(baner==NULL) baner = b = new bane;
else { b->neste = new bane; b=b->neste; }
if (!(ui >> b->fork >> b->type >>....>> b->ist))
break; // don't add this last record, it's state is invalid
}
ui.close();
Though I'd prefer using getline to read a line into a string and then
parse with stringstream.
> I am trying to convert from a half-automated system using MS Excel by
> converting the Excel table to text and reading the text file into the
> C++ program. Not quite decided on how to store the data in the C++
> system yet. It consists of several tables, corresponding to several
> classes in C++.
Consider using CSV. If your data is not too big, then it's a
semi-standard, nicely readable format that's pretty easy to manage.
--
Tamas Marki