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. C / C++ / MFC
  4. files stream & iterators

files stream & iterators

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
9 Posts 8 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.
  • G Offline
    G Offline
    ganesh_IT
    wrote on last edited by
    #1

    Hi, how to use iterators in file stream can any one explain me with sample code, that read and write files using iterator

    C M S A N 5 Replies Last reply
    0
    • G ganesh_IT

      Hi, how to use iterators in file stream can any one explain me with sample code, that read and write files using iterator

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Please explain what you are trying to achieve. Files and iterators are two completely different concepts.

      Cédric Moonen Software developer
      Charting control [v3.0] OpenGL game tutorial in C++

      R 1 Reply Last reply
      0
      • G ganesh_IT

        Hi, how to use iterators in file stream can any one explain me with sample code, that read and write files using iterator

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        something like that (snatch from a google search):

        std::ifstream ifs("filename.txt");
        std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());

        ?

        Watched code never compiles.

        P 1 Reply Last reply
        0
        • G ganesh_IT

          Hi, how to use iterators in file stream can any one explain me with sample code, that read and write files using iterator

          S Offline
          S Offline
          Sauro Viti
          wrote on last edited by
          #4

          Try this[^] link...

          1 Reply Last reply
          0
          • C Cedric Moonen

            Please explain what you are trying to achieve. Files and iterators are two completely different concepts.

            Cédric Moonen Software developer
            Charting control [v3.0] OpenGL game tutorial in C++

            R Offline
            R Offline
            Rob Grainger
            wrote on last edited by
            #5

            Really? I was under the impression that istream_iterator and ostream_iterator were for exactly that purpose.

            C 1 Reply Last reply
            0
            • R Rob Grainger

              Really? I was under the impression that istream_iterator and ostream_iterator were for exactly that purpose.

              C Offline
              C Offline
              Cedric Moonen
              wrote on last edited by
              #6

              Looking at the other replies, I realized that I misunderstood his question. I thought that he wanted to serialize a container and use the iterators to save the data. that's why I asked him more information.

              Cédric Moonen Software developer
              Charting control [v3.0] OpenGL game tutorial in C++

              1 Reply Last reply
              0
              • G ganesh_IT

                Hi, how to use iterators in file stream can any one explain me with sample code, that read and write files using iterator

                A Offline
                A Offline
                Aescleal
                wrote on last edited by
                #7

                At the risk of sounding glib, you can use stream iterators with any stream, whether they're from memory, sockets, files. So if I have a lump of integers in a vector:

                int init_data[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
                std::vector<int> data( &int_data[0], &int_data[10] );

                I can pump them out to any stream using the copy algorithm. So:

                std::copy( data.begin(), data.end(), std::ostream_iterator<int>( std::cout, "\n" ) );

                will write that lot out to std::cout separated by newlines. If you replace std::cout with the name of an ofstream then those values will tbe written to the file instead. Replace it with the name of an ostringstream and the data will end up in a string. To read from a stream is much the same, except you use an istream_iterator:

                std::vector<int> data;
                std::copy( std::istream_iterator<int>( std::cin ), std::istream_iterator<int>(), std::back_inserter( data ) );

                will read integers from standard in until something goes wrong with the stream (end of file, I/O error, run out of memory) and dump them at the back of data. And of course it's not just copy - you can use any std algorithm that can handle input/output iterators. So as two examples you can transform a file into another one or accumulate the values in another. Basically any single pass operation you can do on a list or vector you can do on a file or pair of files in the same manner. Cheers, Ash

                1 Reply Last reply
                0
                • G ganesh_IT

                  Hi, how to use iterators in file stream can any one explain me with sample code, that read and write files using iterator

                  N Offline
                  N Offline
                  Niklas L
                  wrote on last edited by
                  #8

                  If you want to learn about iterators, I can recommend that you implement a few. It's a great way of learning what it is, the difference between the various types, forward/random access/bidirectional/etc, and in what situations an iterator is a good or bad choice. It's also a great lesson in efficiency as well as a way of getting to know stl in general a bit better.

                  home

                  1 Reply Last reply
                  0
                  • M Maximilien

                    something like that (snatch from a google search):

                    std::ifstream ifs("filename.txt");
                    std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());

                    ?

                    Watched code never compiles.

                    P Offline
                    P Offline
                    prasad_som
                    wrote on last edited by
                    #9

                    Maximilien wrote:

                    std::ifstream ifs("filename.txt");std::string str((std::istreambuf_iterator(ifs)), std::istreambuf_iterator());

                    Something like this is much faster,

                    std::ifstream stream("c:\\test.txt");
                    if (stream.good())
                    {
                    std::stringbuf buf;
                    stream>>&buf;
                    std::string str(buf.str());
                    stream.close();
                    }

                    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