 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Thu Aug 24, 2006 9:10 am Post subject: how to copy a part of a binary file |
|
|
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
*/
} |
|
| Back to top |
|
 |
Jens Theisen Guest
|
Posted: Thu Aug 24, 2006 9:10 am Post subject: Re: how to copy a part of a binary file |
|
|
pedagani (AT) gmail (DOT) com wrote:
| Quote: | 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 |
|
| Back to top |
|
 |
Guest
|
Posted: Wed Sep 20, 2006 9:10 am Post subject: Re: how to copy a part of a binary file |
|
|
Jerry Coffin wrote:
| Quote: | In article <1156397620.305575.142870 (AT) 75g2000cwc (DOT) googlegroups.com>,
pedagani (AT) gmail (DOT) com says...
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.
--
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. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|