473,807 Members | 2,877 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.bi n",ios_base::bi nary);
ofstream os("c:\\testpar se.bin",ios_bas e::binary);
if (!is || !os)
{
cout<<"Could not open the file for read/write";
exit(1);
}

is.seekg(START_ OFFSET_,ios::be g);

copy(istream_it erator<char(is) ,
istream_iterato r<char(),
ostream_iterato r<char(os));

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

/* I wanted to have something like this
//basic_istream<c har,char_traits <char& start =
is.seekg(START_ OFFSET_,ios::be g);
//basic_istream<c har,char_traits <char& end =
is.seekg(END_OF FSET_,ios::beg) ;
//copy( istream_iterato r<char(start),
// istream_iterato r<char(end),
// ostream_iterato r<char(os)); //Apparently, this does not work
*/

}

Aug 24 '06 #1
4 6035
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************ **********@75g2 000cwc.googlegr oups.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_OFF SET);
const std::ifstream:: fpos stop(END_OFFSET +1);

std::copy(istre am_iterator<cha r>(is),
isstream_iterat or<char>(is)+ stop-start,
ostream_iterato r<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************ **********@75g2 000cwc.googlegr oups.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_OFF SET);
const std::ifstream:: fpos stop(END_OFFSET +1);

std::copy(istre am_iterator<cha r>(is),
isstream_iterat or<char>(is)+ stop-start,
ostream_iterato r<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
1772
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 source-file. thx. std::istream_iterator<std::string> ins(in), eos; std::ostream_iterator<std::string> outs(out); std::copy(ins, eos, outs);
5
8042
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 the JPEG file to the PDF (intended as a text file); I've tryied with the FileStream class, but it won't work, maybe bacause, somehow, the binary data "changed" during the read/write process. Is there any method to copy the data without changing...
5
1911
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 copy a logical file (a part of the fysical file) into a new file. But the 'big' problem is: these records contains bankstatements, so I can't take the risk that there would be a record missing, or even one little charachter. The copy I make has to...
7
11647
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 is proving to be more difficult. These pictureboxes are bound to an AccessDB. If the user wants to add an image, they select an image using an OpenFileDialog: Dim result As DialogResult = Pic_Sel.ShowDialog() If (result = DialogResult.OK) Then
9
10039
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) from scratch in Sybase and then uses the Sybase bulk copy tool "bcp" to copy the data in. I already did a brute force port of this script to postgres once, but I'm trying to do it more elegantly now that I know what issues I'm going to run...
4
2775
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 fileName As String Public fileExtention As String Public filePath As String Public newName As String
4
5712
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 files (something like ftp server/client). I implemented all the commands which the server executes from scratch meaning I don't use fork and exec. It was very educational but my problem is that when I try to "get <filename>" from server to the...
1
2714
by: Jassim Rahma | last post by:
how can I copy a file to a folder with displaying a progressbar?
3
8398
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 it may open one more file in binary mode like:-
0
10372
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10112
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6879
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.