Okay so I am working on an external polyphase sort/merge and I am using peek to look at the next value in a file, there is just one problem. My peek gives me the wrong number, but if i just read the value it gives me the right value. What could be wrong?

Recommended Answers

All 3 Replies

peek() reads a single character from the text file -- it can't be used to read integers except that it read a single digit.

is there anyway i can peek() and get a 2 or more digit number?

You cannot peek for more than one character. However, you can use the tellg and seekg functions to restore the original position after having done a multi-character reading. As so:

int peek_int(std::istream& in) {
  std::streampos orig_pos = in.tellg();
  int result = 0;
  in >> result;
  in.seekg(orig_pos);
  return result;
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.