473,382 Members | 1,407 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Is this approach safe?

Hi all,

I am filling up a class by reading a binary file.
I have a Read(std::string& name) method that opens the file and reads
the inside.
The first few lines of the file contain some text describing the size of
the data that follows; imagine that the file contains some images and
this first line tells the size of the images and their number.

Having this info, what I do is to call a function Init() that allocates
all the necessary memory to be filled up with the raw data coming from
the file. In this way, being always inside the method Read(), I start to
read the actual values only after that the function Init() has returned.

The stream remains open during the call to Init(). I was now thinking:
what if for some reason, when the function returns, the stream has been
closed and the loading of the values fails miserably?

How would you suggest to tackle this problem in a safer way? One way
would be to have two different files: the first with the info about the
data contained in the second one. I am sure there is a better way though.

Thanks.
Jul 23 '07 #1
4 1170
Giff wrote:
I am filling up a class by reading a binary file.
I have a Read(std::string& name) method that opens the file and reads
the inside.
The first few lines of the file contain some text describing the size
of the data that follows; imagine that the file contains some images
and this first line tells the size of the images and their number.

Having this info, what I do is to call a function Init() that
allocates all the necessary memory to be filled up with the raw data
coming from the file. In this way, being always inside the method
Read(), I start to read the actual values only after that the
function Init() has returned.
The stream remains open during the call to Init(). I was now thinking:
what if for some reason, when the function returns, the stream has
been closed and the loading of the values fails miserably?
Closing of a stream is an operation on the stream variable. *Whad* can
close *your* stream if *you're* holding the reins? *What* reason can it
possibly be?

Now, if you have been reading from a removable storage (like a CD or
a floppy disk) and then the user pushes the "eject" button on the front
of the computer during reading, this usually leads to _an_exceptional_
situation. Handling those is what *C++ exceptions* are for.
How would you suggest to tackle this problem in a safer way? One way
would be to have two different files: the first with the info about
the data contained in the second one. I am sure there is a better way
though.
If the read operation fails, stop reading, set the "bad state of the
object" flag and return. The caller should verify that the data have
been properly read from the file (stream).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 23 '07 #2
Victor Bazarov wrote:
Closing of a stream is an operation on the stream variable. *Whad* can
close *your* stream if *you're* holding the reins? *What* reason can it
possibly be?

OK, I was thinking that the OS could close the stream in case it would
need to.

Handling those is what *C++ exceptions* are for.

So, do you think that exception handling is superfluous in my case (I
read from the HD)?

>
If the read operation fails, stop reading, set the "bad state of the
object" flag and return. The caller should verify that the data have
been properly read from the file (stream).

Thanks Victor.
Jul 23 '07 #3
Giff wrote:
Victor Bazarov wrote:
>Closing of a stream is an operation on the stream variable. *Whad*
can close *your* stream if *you're* holding the reins? *What*
reason can it possibly be?


OK, I was thinking that the OS could close the stream in case it would
need to.
I don't think OS is aware of *your* streams. The underlying file can
of course be deleted, for example, or the admin can change the access
rights and so on, but dealing with those things is really in the realm
of the OS (rather than the language)...
>Handling those is what *C++ exceptions* are for.


So, do you think that exception handling is superfluous in my case (I
read from the HD)?
No, I actually think that you should essentially throw some I/O-specific
exception if, beyond any normal functioning, your program fails to read
from the file it just opened for reading (and began reading successfully)
and should be able to finish.

Now, there are degrees of problems your I/O code can encounter and you
need to decide at what point to consider it normal "error handling" and
an what point to just thrown "hands in the air", so to speak, and declare
the situation so unusual that nobody can predict it. For example, if the
file you're given has at least enough bytes to probably contain whatever
you're about to read from it, it's fine, and if it's size is significantly
smaller than what should be there, you can predict errors before trying
to read from that file. If the OS closes the file your own program deemed
OK beforehand, during your reading of the file, it's an exceptional case,
and not "normal" or "expected" (if any error can be called that).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 23 '07 #4
"Giff" <gi******@gmail.com.invalidwrote in message
news:f8**********@aioe.org...
Hi all,

I am filling up a class by reading a binary file.
I have a Read(std::string& name) method that opens the file and reads the
inside.
The first few lines of the file contain some text describing the size of
the data that follows; imagine that the file contains some images and this
first line tells the size of the images and their number.

Having this info, what I do is to call a function Init() that allocates
all the necessary memory to be filled up with the raw data coming from the
file. In this way, being always inside the method Read(), I start to read
the actual values only after that the function Init() has returned.

The stream remains open during the call to Init(). I was now thinking:
what if for some reason, when the function returns, the stream has been
closed and the loading of the values fails miserably?

How would you suggest to tackle this problem in a safer way? One way would
be to have two different files: the first with the info about the data
contained in the second one. I am sure there is a better way though.
As Victor says correctly, this would be a case I would throw an exception.
If your funciton is given a filename and is expected to read its contents
and return them somehow, if it can not then this is an error. It *should*
work. If it doesn't then what can you expect the code that called Read to
do? Not much. So throw. Whatever called Read then can decide to catch the
except and try something different, or let it proprate up til something
catches it or the program terminates.

If it is not real iimportant that this file is read, then whatever calls
read can catch it and do something else. If it is extremely important,
whatever calls it can still catch it if it wants and display a diagnostis
message and throw itself.

It should throw for a number of reasons. 1. It can't open the file 2. The
file closes before it's finished reading. 3. The data length is not correct
You should probably give the reason the read failed in what you throw, some
time of message or constant.
Jul 24 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Perttu Pulkkinen | last post by:
I ask all php-freaks say how they feel this CMS approach especially from the point of view of 1) admin usability, 2) user rights management and) database speed. In my approach webcontent could...
9
by: Jody Gelowitz | last post by:
I am trying to find the definition of "Safe Printing" and cannot find out exactly what this entitles. The reason is that I am trying to print contents from a single textbox to no avail using the...
6
by: Paul | last post by:
Hi. Just trying to find out the best approach as I beleive it might give me problems later on down the road. I have an ASP.NET application which references a shared database class which...
11
by: dee | last post by:
OleDbCommand class like many .NET classes has the following description in its help file: "Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for...
2
by: Benoit Martin | last post by:
I'm developping a windows app that will be sold to different clients. The app connects to a Database located on my server. That app is installed on multiple machines for each client. Each client...
0
by: gm | last post by:
Immediately after generating the Access application from the Source Safe project I get: "-2147467259 Could not use ''; file already in use." If Access database closed and then reopened I get:...
1
by: johnlim20088 | last post by:
Hi, Currently I have 6 web projects located in Visual Source Safe 6.0, as usual, everytime I will open solution file located in my local computer, connected to source safe, then check out/check in...
5
by: jehugaleahsa | last post by:
Hello: I am trying to find what is the very best approach to business objects in Windows Forms. Windows Forms presents an awesome opportunity to use DataTables and I would like to continue doing...
3
by: Baris-C | last post by:
Here is an approach for asthma treatment nothing about php however, it would be helpful for asthma sufferers. Summary: Hyperventilation - OverBreathing, cause disturbance of various gases,...
20
by: mike3 | last post by:
Hi. (Xposted to both comp.lang.c++ and comp.programming since I've got questions related to both C++ language and general programming) I've got the following C++ code. The first routine runs in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.