Skip to main content
gamedev.net
🔒 Locked

istream_iterator

Started by godmodder Aug 22, 2008 at 3:34 PM 1 replies 750+ views
Original Post
godmodder
godmodder
Hello, I want to read in a fixed amount of ASCII records from a file. I would like to know if I could do this using an STL algorithm. I know you can do the following: copy(istream_iterator(m_File), istream_iterator(), back_inserter(Book.m_Chapters)); What I would really like to do is something like this (doesn't work): copy(istream_iterator(m_File), istream_iterator(m_File) + NumOfChapters, back_inserter(Book.m_Chapters)); Is there a way to achieve this behavior or is there a better solution I don't know of? Thanks in advance, Jeroen
ToohrVyk
ToohrVyk
No, this is not possible.

You can look into std::copy_n (which is an SGI extension) or reimplement it yourself:

template <typename In, typename Out>typename std::iterator_traits<In>::difference_type copy_n(In begin, In end, Out out,        typename std::iterator_traits<In>::difference_type count){  typename std::iterator_traits<In>::difference_type diff = 0;  while (diff != count && begin != end)   {    *out = *begin;    ++out;    ++begin;    ++count;  }  return diff;}
Mike nl
Mike nl
Surely that should be ++diff; instead of ++count; ?
Million-to-one chances occur nine times out of ten!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.