Solved

file exception

Posted on 2001-07-15
13
180 Views
Last Modified: 2013-11-20
hi experts,

i need to read 2 columns of foating points feagures from a text files, something like this:

2.3424   564.685
3.5465   45.567
and so on...

there is at least one space between 2 feagures and the floating point number is unknown which means that it might be one number after floating point and might be 4 numbers after floating point.

i'm using this code right now:

     ifstream in(strFileName.GetBuffer(0));

     double mass, score;
     CArray<double,double> arrMass, arrScore;
     
     while(!in.eof())
     {
          in >> mass >> score;
          arrMass.Add(mass);
          arrScore.Add(score);
     }

the problem is that i wanna use file exception in order to catch reading exceptions, such as string instead of floating points feagures, or more than 2 floating points feagures in one row.

what is the best way to achieve it?
0
Comment
Question by:sedgwick
  • 7
  • 4
  • 2
13 Comments
 
LVL 42

Author Comment

by:sedgwick
ID: 6283156
in case of courrupted file the 'while(!in.eof()' cause endless loop...
         
0
 
LVL 2

Expert Comment

by:curri
ID: 6283307
You can use in.fail() instead of in.eof()

in.fail() returns true if the operation failed, for any reason.

Orlando
0
 
LVL 22

Expert Comment

by:nietod
ID: 6283321
By default stream objects do not throw exceptiosn on error conditions.  However, the exeptions() member funciton can be used to set flags for the types of error conditions on which exceptions should be thrown.  for example, when a formatted extraction fails due to invalid input, it is considered a "fail" error.  This usualy results only in the fail bit being set, but if the stream is set  to throw exceptions on this error, an exception will also be thrown.

ifstream in(strFileName.GetBuffer(0));

double mass, score;
CArray<double,double> arrMass, arrScore;

in.exceptins(ios_base::failbit)
                   

try
{
   while(!in.eof())
   {
      in >> mass >> score;
      arrMass.Add(mass);
      arrScore.Add(score);
   }
}
catch (const ios_base::failure &f)
{
}
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 42

Author Comment

by:sedgwick
ID: 6283523
nietod: then why it keeps looping on an on?
0
 
LVL 42

Author Comment

by:sedgwick
ID: 6283527
besides i used your code and it never reached to the 'catch' point.

the endless loop takes place when the file looks like this:
2.45 34.676
345 6234rtbe rtb
reg.rger 546g erb54

i want the loop to stop if he can't assign readen text from the file to the floating points values...
0
 
LVL 22

Expert Comment

by:nietod
ID: 6283628
it should work.  It works for me in my tests.

Did you remember to set the exceptiojn flags with the exceptions() function?

Post the exact code you used.
0
 
LVL 42

Author Comment

by:sedgwick
ID: 6283699
i copyied your code as is, but it kept running.
0
 
LVL 42

Author Comment

by:sedgwick
ID: 6283764
curri: thanks for your comment, i managed to handle the reading properly but i realy like to know how to determine the reason of the failure?

0
 
LVL 22

Expert Comment

by:nietod
ID: 6284086
the fail bit is set only from one cause.  When a formatted read encounters input that is invalid for the data type to be read.  For example, if you try to read an integer and float and the first character read is not a digit.

This will set the failbit flag and depending on your current exceptions settings will throw an exception.

Why didn't you post your code?
0
 
LVL 42

Author Comment

by:sedgwick
ID: 6284913
what is this line? - in.exceptins(ios_base::failbit)
                 

0
 
LVL 2

Expert Comment

by:curri
ID: 6285222
Hi sedgwick:

If fail() and not eof(), then you know that something strange happened. Most probably bad inputs (hardware errors are very rare).

I can't find any way of getting more info about the error (do you know any nietod?) and it kinda makes sense. It seems to be hard to find errors that apply to any kind of file (hard disks, removable media, network filesystems), and not worth the trouble.

Of course, you can always read character by character and form the floats yourself, but that's probably more trouble that you want :)

I think also, if you are reading from an fstream, there may be a way of getting to the underlying C or even OS file, and you can get info from there.

Orlando
0
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 6285357
>> what is this line? - in.exceptins(ios_base::failbit)
As i said, by default the stream object does NOT throw an exception when it reads invalid data.  You said you wanted it to throw an exception when it reads invalid data.  That line sets the stream's exception mask to indicte that an exception should be thrown when it reads invalid data.    Note that a 'o' is missing, it should read "exceptions".

0
 
LVL 42

Author Comment

by:sedgwick
ID: 6285615
thanks, workd just fine
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Login Script to Copy Folders 12 42
Define unique primary key 9 95
sumHeights  challenge 17 74
How do I get Window Title of all opened process? 4 14
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question