Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!mips!kpc.com!ardent!jss From: jss@gold.kpc.com (Jerry Schwarz) Newsgroups: comp.lang.c++ Subject: Re: Binary Search Using seekg/tellg Message-ID: Date: 10 Apr 91 01:41:04 GMT References: <71329@brunix.UUCP> Sender: uucp@kpc.com (UNIX-to-UNIX Copy) Organization: Kubota Pacific Computer Lines: 21 In-Reply-To: sdm@cs.brown.edu's message of 8 Apr 91 21:57:32 GMT Nntp-Posting-Host: gold In article <71329@brunix.UUCP> sdm@cs.brown.edu (Scott Meyers) writes: 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? The iostream interface doesn't allow you do to this because it can't be done portably. On operating systems with record oriented file systems there is just no way of doing arbitrary arithmetic on file positions. If you aren't worried about portability to such systems and the system you're on defines |streampos| as an integral type (as some do) then you can do arithmetic to your hearts content. Jerry Schwrz