Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. std::copy question [modified]

std::copy question [modified]

Scheduled Pinned Locked Moved ATL / WTL / STL
questionalgorithmstutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    followait
    wrote on last edited by
    #1

    #include <algorithm>
    #include <fstream>
    #include <sstream>

    int main()
    {
    std::fstream fs("d:/test.txt");
    std::stringstream ss;
    ss << "hello" << std::endl;
    std::copy(std::istream_iterator<char>(ss),
    std::istream_iterator<char>(),
    std::ostream_iterator<char>(fs));
    fs.close();
    return 0;
    }

    The code not work, I have some questions: 1. What does this mean: std::istream_iterator() ? 2. Is the position of the stream iterator depent on the current postion of the stream itselft? Thanks. 3. How to make the code work?

    modified on Wednesday, November 4, 2009 9:13 AM

    S 1 Reply Last reply
    0
    • F followait

      #include <algorithm>
      #include <fstream>
      #include <sstream>

      int main()
      {
      std::fstream fs("d:/test.txt");
      std::stringstream ss;
      ss << "hello" << std::endl;
      std::copy(std::istream_iterator<char>(ss),
      std::istream_iterator<char>(),
      std::ostream_iterator<char>(fs));
      fs.close();
      return 0;
      }

      The code not work, I have some questions: 1. What does this mean: std::istream_iterator() ? 2. Is the position of the stream iterator depent on the current postion of the stream itselft? Thanks. 3. How to make the code work?

      modified on Wednesday, November 4, 2009 9:13 AM

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      Firstly - a CP/HTML thing - you need to escape the < characters in the template parameter spec of the iterators (looking at the source of the page, they should be <char>, yes?). Try declaring fs as a std::ofstream or making sure that the destination file exists - with that change, the code works fine. std::istream_iterator<char>() is a sentinel value that represents the end of any input stream. Once an istream_iterator<char> steps past the end of the file, it has the same value as std::istream_iterator<char>(). A stream iterator interacts with the stream position. It's a bad idea to manipulate the stream while there are active stream iterators.

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      F 1 Reply Last reply
      0
      • S Stuart Dootson

        Firstly - a CP/HTML thing - you need to escape the < characters in the template parameter spec of the iterators (looking at the source of the page, they should be <char>, yes?). Try declaring fs as a std::ofstream or making sure that the destination file exists - with that change, the code works fine. std::istream_iterator<char>() is a sentinel value that represents the end of any input stream. Once an istream_iterator<char> steps past the end of the file, it has the same value as std::istream_iterator<char>(). A stream iterator interacts with the stream position. It's a bad idea to manipulate the stream while there are active stream iterators.

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        F Offline
        F Offline
        followait
        wrote on last edited by
        #3

        Very helpful, thanks.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups