We help IT Professionals succeed at work.
Get Started

Runtime error with decompress program.

PDamasceno
PDamasceno asked
on
995 Views
Last Modified: 2012-05-09
Hey Experts.

When I try to run my program I get this error:
terminate called after throwing an instance of 'std::ios_base::failure'
  what():  basic_filebuf::underflow error reading the file

I can't find why this is happening.
This program scans a directory for the files there and decompress all of them.
It doesn't have any file type filter because I'm just testing this directory scan code to make it part of this decompress program.

I tried to use GDB to find the answer but that didn't went well either.
Here is the last lines executed from GDB:

46          operator T& () const { return *t_; }
(gdb)
boost::iostreams::detail::read_device_impl<boost::iostreams::streambuf_tag>::read<boost::iostreams::filtering_streambuf<boost::iostreams::input, char, std::char_traits<char>, std::allocator<char>, boost::iostreams::public_> > (t=..., s=0x806aa90 "", n=4096)
    at /usr/include/boost/iostreams/read.hpp:153
153                     0;
(gdb)
terminate called after throwing an instance of 'std::ios_base::failure'
  what():  basic_filebuf::underflow error reading the file

Program received signal SIGABRT, Aborted.
0x0067f402 in __kernel_vsyscall ()

The code to the program is attached.
I'm trying to figure this out on my own for a while now, but I got to a point that I need to get help.
Thanks in advance.
#include <fstream>
#include <iostream>
#include <sys/types.h>
#include <dirent.h>

#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/zlib.hpp>
#include <boost/iostreams/filtering_stream.hpp>

using namespace std;
using namespace boost::iostreams;

void decompress (string source, string filename)
{   
    ifstream file(source.c_str(), ios_base::in | ios_base::binary);
    ofstream out(filename.c_str(), ios_base::out | ios_base::binary);
    filtering_streambuf<input> in;
    in.push(zlib_decompressor());
    in.push(file);
    boost::iostreams::copy(in, out);
}

int main ()
{
    string source;
    string filename;
    char result;

    DIR* dir = opendir("/home/pablo/testee");
    struct dirent* ent = 0;

    while (dir && (ent = readdir(dir))) {
         filename = ent->d_name;
         string new_filename = filename;
         result = new_filename.find_last_of('.');

         if (std::string::npos != result)
         {
            new_filename.erase(result);
         }

         filename.append(".txt");

         decompress(ent->d_name, filename);
    }

    closedir(dir);
    
    return 0;
}

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Top Expert 2009
Commented:
This problem has been solved!
Unlock 6 Answers and 52 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE