Lars Finsen 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.
> 
>> 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();
> 
> It reads all the actual lines correctly, but then goes on to read  
> endless null lines. The file does contain linefeeds, but as far as I  
> have understood, these are skipped when the skipws flag is set, which  
> (as far as I have understood) it is by default.
> 
> 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++.
> 
> LEF

Lars, have you looked at using COM to completely automate whatever 
process you are doing?  Excel has a pretty extensive COM automation 
system exposed...although it uses IDispatch, which isn't C/C++ friendly 
(IDispatch is VB-friendly, though).  Then you wouldn't have to deal with 
export/import issues.

I would recommend BString (from Safe C++ Design Principles - see 
c-prog's Files section) but, IIRC, you said something about Borland C++ 
in your first post.  BString depends on Block which, in turn, uses 
'placement new' and 'placement delete'.  The latter are two advanced and 
dangerous C++ concepts, which Borland's compiler didn't fully support 
the last time I checked.  If you try it and it _does_ work, you could do 
this:

size_t z;
BString Data, CurrLine;
Block<BString> SplitData;
List<bane> BaneList;
bane b;

if (!Data.LoadFromFile(filename))
{
   // Barf an error message.

   return 1;
}
z = 0;
CurrLine = Data.LineInput();
while (z < Data.ftell())
{
   // Process line.
   CurrLine.Explode(SplitData, '\t');
   b.fork = SplitData[0];
   b.type = SplitData[1];
   ...
   b.ist = SplitData[???];
   BaneList += b;

   z = Data.ftell();
   CurrLine = Data.LineInput();
}

(NOTE:  LoadFromFile() dumps the entire file into memory.  A good enough 
solution for files smaller than 1MB...perhaps up to 2MB.  Then again, 
the code isn't exactly designed for performance as it is to just simply 
work as a quick-n-dirty first pass solution.  Don't know why you need a 
linked list, but you shouldn't be rolling your own.)

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* VerifyMyPC 2.0
Change tracking and management tool.
Reduce tech. support times from 2 hours to 5 minutes.

Free for personal use, $10 otherwise.
http://www.CubicleSoft.com/VerifyMyPC/

Reply via email to