455,434 Members | 1,858 Online
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

# distance between stream iterators

 P: n/a 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 #include #include #include #include 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 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 first_byte(f); istream_iterator foundAt; foundAt= std::search( istream_iterator(f), istream_iterator (), 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 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::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; } Nov 22 '05 #1
4 Replies

 P: n/a On 2005-11-14, fa********@cox.net wrote: 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. -- Neil Cerutti Nov 22 '05 #2

 P: n/a "Neil Cerutti" wrote in message news:sl**********************@FIAD06.norwich.edu.. . On 2005-11-14, fa********@cox.net wrote: 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. I just typed in win win and my puter crashed. Repair cost was O log n. Where do I send the Bill? Sorry. -Mike Nov 22 '05 #3

 P: n/a 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! Nov 22 '05 #4

 P: n/a 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. Nov 22 '05 #5

### This discussion thread is closed

Replies have been disabled for this discussion.