Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!brunix!sdm From: sdm@cs.brown.edu (Scott Meyers) Newsgroups: comp.lang.c++ Subject: Binary Search Using seekg/tellg Message-ID: <71329@brunix.UUCP> Date: 8 Apr 91 21:57:32 GMT Sender: news@brunix.UUCP Reply-To: sdm@cs.brown.edu (Scott Meyers) Organization: Brown University Department of Computer Science Lines: 43 Regarding direct access in input streams, the istream man page says: insp=&ins.seekg(off,dir) Repositions ins.rdbuf()'s get pointer. See sbuf.pub(3C++) for a discussion of positioning. insp=&ins.seekg(pos) Repositions ins.rdbuf()'s get pointer. See sbuf.pub(3C++) for a discussion of positioning. pos=ins.tellg() The current position of ios.rdbuf()'s get pointer. See sbuf.pub(3C++) for a discussion of position- ing. Okay, this is from the sbuf.pub man page: pos=sb->seekpos(pos, mode) Repositions the streambuf get and/or put pointer to pos. mode specifies which pointers are affected as for seekoff(). Returns pos (the argument) or EOF if the class does not support repositioning or an error occurs. In general a streampos should be treated as a "magic cookie" and no arithmetic should be performed on it. So how do I implement a binary search using iostream I/O? I need to be able to get two stream positions (corresponding to the top and bottom of the region I'm about to bisect) that I can perform arithmetic on. For example, during the first iteration of the search, I want to go to the top of the file and get a streampos, call it TOP, then to the BOTTOM of the file to get another streampos, call it BOTTOM, then I want to jump to the position in the file corresponding to (BOTTOM-TOP)/2. However, the man page says that streampos objects don't have to obey the laws of arithmetic. What do I do? Thanks, Scott ------------------------------------------------------------------------------- What do you say to a convicted felon in Providence? "Hello, Mr. Mayor."