Ok simple question. Google didn't bring up much and I need this.
This is the question: How do I interact with non-text files in a simple console app?

Recommended Answers

All 5 Replies

Depends on what you mean by interact. Nothing kinky I hope.

I want to ether copy it to another location or (more likely) send it through zlib's deflate.

To copy to another location, open the file in binary mode then read/write 255 bytes at a time until end-of-file is reached. Another way to do it on MS-Windows is call win32 api function CopyFile()

To copy to another location, open the file in binary mode then read/write 255 bytes at a time until end-of-file is reached. Another way to do it on MS-Windows is call win32 api function CopyFile()

I'd use 256. It fits better in the realm of binary. Any power of 2 would work.

> I want to ether copy it to another location

#include <fstream>

int main()
{
    std::ifstream fin( "myfile.dat", ios_base::in|std::ios_base::binary ) ;
    std::ofstream fout( "myfile.cpy", ios_base::out|std::ios_base::binary ) ;
    fout << fin.rdbuf() ;
}

> or (more likely) send it through zlib's deflate.

Simplest if you use a C++ wrapper over zlib.
For example, using the Boost Iostream library http://www.boost.org/doc/libs/1_47_0/libs/iostreams/doc/index.html

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

int main()
{
    std::ifstream fin( "myfile.dat", ios_base::in|std::ios_base::binary ) ;
    std::ofstream fout( "myfile.z", ios_base::out|std::ios_base::binary ) ;

    boost::iostreams::filtering_streambuf< boost::iostreams::output > stmbuf ;
    stmbuf.push( boost::iostreams::zlib_compressor() ) ;
    stmbuf.push(fout) ;

    boost::iostreams::copy( fin, stmbuf );
}
commented: good suggestions :) +17
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.