distance between stream iterators

F

fastback66

Evidently per the C++ standard, it is not possible to measure the
distance between two stream iterators, because two non-end-of-stream
iterators are equal when they are constructed from the same stream. I
still don't quite understand why that fact is true. But...

I wish to find the first occurance of some sequence in an existing
stream. For the moment this stream is a file on disk, but later may be
a stream from another process. Anyway, if I read a chunk of the stream
into a container like a vector, I can use the STL search() algorithm to
get an iterator to the sequence being searched for, and then use the
STL distance() algorithm to measure the distance between this iterator
and the beginning of the vector. The beginning of the vector is
directly related to the beginning of the stream, so I know where the
sequence is in the stream. So you'd think I'd be happy. Well sort of.

I'd like to operate directly on the stream, without re-buffering the
data into another container. This seems like a reasonable desire, since
the stream is already buffered by its internal implementation. But the
STL standard for some reason prevents the comparision of istream
iterators. Why? Or more importantly how can I operate directly on the
stream, and get an integer value of the location of the desired
sequence. Sure I have it solved with a vector approach, but that seems
an indirect and unnecessary approach. Below is my test code for this
inquiry.

//stream iterator test
#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>
#include <vector>

using namespace std;
int main()
{
// FAF03200 can be found at byte location 28 in test.bin
// let's see if we can get those same results by operating
// directly on the on the stream, instead of copying first
// into some container.

vector<unsigned char> pattern; // a pattern in file

// store FAF03200 pattern in a sequence vector
std::inserter(pattern,pattern.end())= (unsigned char)0xF0;
std::inserter(pattern,pattern.end())= (unsigned char)0xFA;
std::inserter(pattern,pattern.end())= (unsigned char)0x00;
std::inserter(pattern,pattern.end())= (unsigned char)0x32;

ifstream f;
string filename("test.bin");
f.open (filename.c_str(), ios::binary );

if ( !(f.good()) ){
cout << "error opening file \n";
return -1;
}

//----------- find first occurance of pattern in file ----------
f.seekg (0, ios::beg);

istream_iterator<unsigned char> first_byte(f);
istream_iterator<unsigned char> foundAt;

foundAt= std::search( istream_iterator<unsigned char>(f),
istream_iterator<unsigned char> (),
pattern.begin(),pattern.end());

cout << "search complete \n";

//ERROR: distance() always returns zero
long at = distance(first_byte, foundAt);

cout << "Stream methods says it's at " << at << ".\n";

//-------------- the indirect method works fine but... ------------
char c;
int numBytes=0;
vector<unsigned char> v;

// throw a chunk of file into a unsigned char vector
f.seekg (0, ios::beg);
while( f.get(c) && numBytes++ < 8192 ){
std::inserter(v,v.end())= c;
}

// find iterator to first sequence in char vector
vector<unsigned char>::iterator pos;
pos = std::search ( v.begin(),v.end(),
pattern.begin(),pattern.end());

// determine index from the return iterator
int where = distance(v.begin(),pos); // GREAT: works perfect

cout << "Indirect vector methods says it's at " << where << ".\n";

f.close();
return 0;
}
 
N

Neil Cerutti

Evidently per the C++ standard, it is not possible to measure
the distance between two stream iterators, because two
non-end-of-stream iterators are equal when they are constructed
from the same stream. I still don't quite understand why that
fact is true. But...

You may wish to try to implement your own istream_iterator-like
template that is a forward iterator instead of an input iterator.
If it turns out to be no trouble, then you'll have what you
wanted. If it turns out that you can't do it, then you'll be
enlightened. That's a win-win.
 
M

Mike Wahler

Neil Cerutti said:
You may wish to try to implement your own istream_iterator-like
template that is a forward iterator instead of an input iterator.
If it turns out to be no trouble, then you'll have what you
wanted. If it turns out that you can't do it, then you'll be
enlightened. That's a win-win.

I just typed in win win and my puter crashed.
Repair cost was O log n. Where do I send the Bill?

Sorry.

-Mike
 
T

twoeyedhuman1111

Distance between stream operators? Why t3h h3ll would you want to know
that?

Give me your ip and maybe I'll show you some of my scripts I
downloaded!
 
F

fastback66

I agree that implementing my own iterator may provide some insight and
would be enlightening, but it will take some thought. I'm concerned
that such an implementation would still involve re-buffering of data
which istream already buffers. The whole point of my concern is
efficiency improvement, by eliminating the copy of data to another
container.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top