how to copy a part of a binary file

P

pedagani

I want to copy only a part of the binary file delimited by offset
values say START_OFFSET_ & END_OFFSET_ which can be as huge as a 50
Giga. Below is the program where I could set a START_OFFSET_ but could
not specify the END_OFFSET_ value.
Any thoughts? Please advise. Thanks.

#include <fstream>
#include <iostream>
#include <algorithm>
#include <ios>
#include <iterator>
#include <string>
#define START_OFFSET_ 5000000
#define END_OFFSET_ 10000000
using namespace std;
void main()
{

ifstream is("c:\\test.bin",ios_base::binary);
ofstream os("c:\\testparse.bin",ios_base::binary);
if (!is || !os)
{
cout<<"Could not open the file for read/write";
exit(1);
}

is.seekg(START_OFFSET_,ios::beg);

copy(istream_iterator<char> (is),
istream_iterator<char> (),
ostream_iterator<char> (os));

is.close();
os.close();

/* I wanted to have something like this
//basic_istream<char,char_traits<char> >& start =
is.seekg(START_OFFSET_,ios::beg);
//basic_istream<char,char_traits<char> >& end =
is.seekg(END_OFFSET_,ios::beg);
//copy( istream_iterator<char> (start),
// istream_iterator<char> (end),
// ostream_iterator<char> (os)); //Apparently, this does not work
*/

}
 
J

Jens Theisen

I want to copy only a part of the binary file delimited by offset
values say START_OFFSET_ & END_OFFSET_ which can be as huge as a 50
Giga. Below is the program where I could set a START_OFFSET_ but could
not specify the END_OFFSET_ value.
Any thoughts? Please advise. Thanks.

Try using the read and write functions of streams instead, they are also
much faster than using iterators that way.

Jens
 
P

pedagani

Jens said:
Try using the read and write functions of streams instead, they are also
much faster than using iterators that way.

Jens

thank you for your reply. speed isn't very imp at this point, i'm
looking for a neat code. ny alternatives? thanks
 
J

Jerry Coffin

I want to copy only a part of the binary file delimited by offset
values say START_OFFSET_ & END_OFFSET_ which can be as huge as a 50
Giga. Below is the program where I could set a START_OFFSET_ but could
not specify the END_OFFSET_ value.

Assuming (for the moment) that your implementation has something like
64-bit ints that have the range to directly address all possible file
sizes:

const std::ifstream::fpos start(START_OFFSET);
const std::ifstream::fpos stop(END_OFFSET+1);

std::copy(istream_iterator<char>(is),
isstream_iterator<char>(is)+ stop-start,
ostream_iterator<char>(os));

There are, however, a fair number of implementations that simply don't
have any standard type with a range as large as the file sizes they
support (and specifically less than the 50 gigabytes you mention). In a
case like this, there's probably no standard/portable code to do what
you want.
 
P

pedagani

Jerry said:
Assuming (for the moment) that your implementation has something like
64-bit ints that have the range to directly address all possible file
sizes:

const std::ifstream::fpos start(START_OFFSET);
const std::ifstream::fpos stop(END_OFFSET+1);

std::copy(istream_iterator<char>(is),
isstream_iterator<char>(is)+ stop-start,
ostream_iterator<char>(os));

There are, however, a fair number of implementations that simply don't
have any standard type with a range as large as the file sizes they
support (and specifically less than the 50 gigabytes you mention). In a
case like this, there's probably no standard/portable code to do what
you want.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Thank you. Although it could not be directly applied for my
application, very insightful solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top