473,396 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
*/

}

Aug 24 '06 #1
4 6013
pe******@gmail.com wrote:
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
Aug 24 '06 #2

Jens Theisen wrote:
pe******@gmail.com wrote:
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
thank you for your reply. speed isn't very imp at this point, i'm
looking for a neat code. ny alternatives? thanks

Aug 24 '06 #3
In article <11**********************@75g2000cwc.googlegroups. com>,
pe******@gmail.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.
Aug 27 '06 #4

Jerry Coffin wrote:
In article <11**********************@75g2000cwc.googlegroups. com>,
pe******@gmail.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.

Sep 20 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Severin Ecker | last post by:
hi! i'm trying to copy a file with the std::copy function but my problem is, that whitespaces are discarded. could anyone tell me how i can just copy all the characters that are in the...
5
by: AlexCDM | last post by:
Greetings; I'm a (young) .NET developer and I have this problem: I'm building an application (with C#) that creates PDF files. To display a JPEG image within the PDF I need to copy the data from...
5
by: DraguVaso | last post by:
Hi, I need a SECURE way to copy parts of a file. I'm having files which contains a whole bunch of records. In one 'fysical' file I'm having one or more logical files. What I need to do is to...
7
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard...
9
by: David Rysdam | last post by:
I have a large amount of data that I copy in and out of Sybase very often. Now I also want to copy this data in and out of postgres. I have an existing script that creates the entire database(s)...
4
by: Kyote | last post by:
I'm trying to persist a list of filenames. I've made a custom collection and a FileName class: 'Class to hold file name information Public Class FileNames Public fullName As String Public...
4
by: pradqdo | last post by:
Hi folks, I have a very strange problem when I try to port my client/server program to cygwin. It is a simple shell program where the server executes client's commands + it can send and receive...
1
by: Jassim Rahma | last post by:
how can I copy a file to a folder with displaying a progressbar?
3
by: BatthJeet | last post by:
Hi all ! Can we have some way to make a program in C-Language which opens an Image(.jpeg/.bmp ) file in binary mode like:- FILE *sourcefile; sourcefile=fopen("d:\\mypic.jpeg","rb"); Then...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.