PDamasceno
asked on
Some problems with Compress/Decompress program.
Hey Experts.
Some days ago I posted a question here for some help with a compress/decompress program.
Got the help I needed and wrote the code below to test with boost iostreams a little bit and finally get to where I want to.
(I'm new to C++ development so it is possible to have syntax erros in the code, point them out for me if you see any please!)
#include <iostream>
#include <sstream>
#include </usr/include/boost/assign /list_of.h pp>
#include </usr/include/boost/iostre ams/copy.h pp>
#include </usr/include/boost/iostre ams/device /back_inse rter.hpp>
#include </usr/include/boost/iostre ams/filter /zlib.hpp>
#include </usr/include/boost/iostre ams/filter ing_stream .hpp>
//------------------------ ---------- ---------- ---------- ---------- ---------- ---
void
compress
(
const std::string& data,
std::string& buffer
)
{
buffer.clear();
boost::iostreams::filterin g_streambu f<boost::i ostreams:: output> out;
out.push(boost::iostreams: :zlib_comp ressor());
out.push(boost::iostreams: :back_inse rter(buffe r));
boost::iostreams::copy(boo st::make_i terator_ra nge(data), out);
}
//------------------------ ---------- ---------- ---------- ---------- ---------- --
void
decompress
(
const std::string& buffer,
std::string& data
)
{
boost::iostreams::filterin g_streambu f<boost::i ostreams:: input> in;
in.push(boost::iostreams:: zlib_decom pressor()) ;
in.push(boost::make_iterat or_range(b uffer));
data.clear();
boost::iostreams::copy(in, boost::iostreams::back_ins erter(data ));
}
//------------------------ ---------- ---------- ---------- ---------- ---------- --
int main()
{
const std::string send_data = "Test, test, test, test!!!!!";
std::string received_data;
std::string buffer;
compress(send_data, buffer);
decompress(buffer, received_data);
if(send_data == received_data)
std::cout << send_data << " equals " << received_data << std::endl;
else
std::cout << "Erro: " << send_data << " differs from " << received_data << std::endl;
return 0;
}
But I keep getting this freaking error while trying to compile it:
testedc.cpp: In function ‘int main()’:
testedc.cpp:45: error: invalid conversion from ‘int’ to ‘const char*’
testedc.cpp:45: error: initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(cons t _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]’
I can't find that invalid conversion it is telling me.
Can you guys help me with this?
Also, I would like you guys to take a look in my code up there and see if everything is fine with it.
A couple more questions:
* How can I make the file go to a specific directory after it is compressed?
* If the file to decompress is not in a buffer but in a directory, how can I tell the function decompress() to work?
* If the path to the file needed be decompressed is stored in other variable, how can I tell the function decompress() to work?
* How can I handle possible exception errors from compress() and decompress()?
* Any thoughts on how to make this program the most efficient possible, in terms of speed of the compress and decompress functions?
Some days ago I posted a question here for some help with a compress/decompress program.
Got the help I needed and wrote the code below to test with boost iostreams a little bit and finally get to where I want to.
(I'm new to C++ development so it is possible to have syntax erros in the code, point them out for me if you see any please!)
#include <iostream>
#include <sstream>
#include </usr/include/boost/assign
#include </usr/include/boost/iostre
#include </usr/include/boost/iostre
#include </usr/include/boost/iostre
#include </usr/include/boost/iostre
//------------------------
void
compress
(
const std::string& data,
std::string& buffer
)
{
buffer.clear();
boost::iostreams::filterin
out.push(boost::iostreams:
out.push(boost::iostreams:
boost::iostreams::copy(boo
}
//------------------------
void
decompress
(
const std::string& buffer,
std::string& data
)
{
boost::iostreams::filterin
in.push(boost::iostreams::
in.push(boost::make_iterat
data.clear();
boost::iostreams::copy(in,
}
//------------------------
int main()
{
const std::string send_data = "Test, test, test, test!!!!!";
std::string received_data;
std::string buffer;
compress(send_data, buffer);
decompress(buffer, received_data);
if(send_data == received_data)
std::cout << send_data << " equals " << received_data << std::endl;
else
std::cout << "Erro: " << send_data << " differs from " << received_data << std::endl;
return 0;
}
But I keep getting this freaking error while trying to compile it:
testedc.cpp: In function ‘int main()’:
testedc.cpp:45: error: invalid conversion from ‘int’ to ‘const char*’
testedc.cpp:45: error: initializing argument 1 of ‘std::basic_string<_CharT,
I can't find that invalid conversion it is telling me.
Can you guys help me with this?
Also, I would like you guys to take a look in my code up there and see if everything is fine with it.
A couple more questions:
* How can I make the file go to a specific directory after it is compressed?
* If the file to decompress is not in a buffer but in a directory, how can I tell the function decompress() to work?
* If the path to the file needed be decompressed is stored in other variable, how can I tell the function decompress() to work?
* How can I handle possible exception errors from compress() and decompress()?
* Any thoughts on how to make this program the most efficient possible, in terms of speed of the compress and decompress functions?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oops, missed the link ;)
http://www.java2s.com/Tutorial/Cpp/0120__Exceptions/Asimpleexceptionhandlingexample.htm
http://www.java2s.com/Tutorial/Cpp/0120__Exceptions/Asimpleexceptionhandlingexample.htm
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the quick answer guys.
The compile problem was something seamless and I really don't know what it was.
Just compiled it and everything went fine. Weird...
As for the other questions, thanks for the insight letharion and Infinity08.
Just to clarify the first question, when I compress a file with boost.iostreams using zlib the file it create, after the compression is done, is something like "filename.z"?
If so I'll just have to use file streams to tell where the file will go?
Sorry if I'm not very clear with my questions and thanks again.
The compile problem was something seamless and I really don't know what it was.
Just compiled it and everything went fine. Weird...
As for the other questions, thanks for the insight letharion and Infinity08.
Just to clarify the first question, when I compress a file with boost.iostreams using zlib the file it create, after the compression is done, is something like "filename.z"?
If so I'll just have to use file streams to tell where the file will go?
Sorry if I'm not very clear with my questions and thanks again.
>> If so I'll just have to use file streams to tell where the file will go?
Indeed. Refer to letharion's example code earlier.
Indeed. Refer to letharion's example code earlier.
It seems pretty good in that it steps through exception handling in several examples, rather than putting everything in the same example :)