file io question

F

Fluke

I'm looking for a way to copy a segment of a binary file.
ie. from start position (>=0) to end position (<= eof).
Any suggestions? (preferably stl)
 
J

John Harrison

Fluke said:
I'm looking for a way to copy a segment of a binary file.
ie. from start position (>=0) to end position (<= eof).
Any suggestions? (preferably stl)

void copy_segment(istream& input, streampos start, streampos end,
vector<char>& output)
{
input.seekg(start);
output.resize(end - start);
input.read(&output[0], end - start);
}

Untested code, and obviously could do with the addition of some error
checking.

john
 
J

John Harrison

John Harrison said:
Fluke said:
I'm looking for a way to copy a segment of a binary file.
ie. from start position (>=0) to end position (<= eof).
Any suggestions? (preferably stl)

void copy_segment(istream& input, streampos start, streampos end,
vector<char>& output)
{
input.seekg(start);
output.resize(end - start);
input.read(&output[0], end - start);
}

Untested code, and obviously could do with the addition of some error
checking.

john

bool copy_segment(istream& input, streampos start, streampos end,
vector<char>& output)
{
vector<char> temp(end - start);
if (!input.seekg(start) || !input.read(&temp[0], end - start))
return false;
output.swap(temp);
return true;
}

Still untested.

john
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top