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;
}
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;
}