0

I implemented a small program that can extract (and via fuse mount) a certain archive format. I use boost::filesystem::ifstream, but on error (e.g. the file a user wants to extract does not exist) I get very nondescript error messages. I wonder is there a way to get better error messages for IO related problems in C++?

On a related note I wonder whether I should have used C's FILE* or in the case of the fuse filesystem just plain file descriptors? Because strerror(errno) is way better than what iostreams are giving me.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
panzi
  • 7,517
  • 5
  • 42
  • 54
  • 1
    Do you mean `std::ifstream` ? AFAIK, there is no `boost::filesystem::ifstream`... – Thanatos May 19 '11 at 22:35
  • 1
    @Thanatos : Boost.Filesystem v2 didn't, but v3 does. See the relevant docs [here](http://www.boost.org/doc/libs/release/libs/filesystem/v3/doc/reference.html#File-streams). – ildjarn May 19 '11 at 23:05

2 Answers2

1

We couldn't find any better way than using boost::iostreams and implementing our own file-based sink and source.

If you want, you can grab the source code here (Apache-licensed):

http://sourceforge.net/projects/cgatools/files/1.3.0/cgatools-1.3.0.9-source.tar.gz/download

the relevant files are:

cgatools/util/Streams.[ch]pp

Igor Nazarenko
  • 2,184
  • 13
  • 10
  • Well, this contains awesome iostream implementations, but I'm not sure if it might be overbloat. Also I'd rather not rip out a few classes from another project and stick it into my own. It's a hell to maintain. If there would be a nice little C++ IO library (that is supported by major Linux distributions) with good error messages and high performance I will gladly use it. But I'm not sure if it isn't better to use basic C IO. I wait for an answer from people who know FUSE better (posted question on FUSE mailinglist). – panzi May 20 '11 at 00:40
  • (I meant overbloat *for my purposes*.) – panzi May 20 '11 at 04:52
0

Since your using the filesystem library anyway, you could test to see if the file exists prior to trying to access it with a stream. This would avoid your bloat concerns, but it would not operate in the same sense as what you're looking for, i.e. the stream itself would not perform the existence check.

However, since you are using boost::filesystem::ifstream, I'm assuming that you are using that because you are using boost::filesystem::path. In boost's implementation of ifstream, they inherit from std::basic_ifstream and override two functions: the constructor and open. So, if you want better error reporting you could simply do they same thing, inherit from boost's implementation and override those two functions to provide the checking you wish. Additional bloat: probably not a lot, and it incorporates the behavior you wish into the stream itself.

rcollyer
  • 10,475
  • 4
  • 48
  • 75