CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2004
    Posts
    3

    Question cout stream manipulations

    Basically, I'm trying to write a progress indicator, first I write two characters as limits, and then go back and fill the space with characters as the situation warrants, vis.

    Code:
    void progress(long n,long x)
    {
       float p=x*0.05, oo=p;
    
       cout<<"[                    ]";
       long pos=cout.tellp();
       cout.seekp(pos-21);
    
       for(n,x ; n<=x ; ++n)
          {
             if( n>oo-1 && n<oo+1 )
                {
                   cout<<"."<<flush;
                   oo=oo+p;
                }
          }
    }
    If I was to instead write that to a file, it would work perfectly.

    It seems that while cout will happily tellp(), it will not obey the directives of a seekp().

    Is the cout stream limited in some way that would prevent normal stream manipulations?

  2. #2
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    std::cout is an output stream. You cannot read from cout, nor can you "rewind" the file pointer.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  3. #3
    Join Date
    Jun 2004
    Posts
    3
    Yet I can successfully perform the same operation with an ofstream object's output stream.

    I don't understand why only some stream manipulations work, for instance, i can read the position of cout's put pointer, but not position it myself.

    It seems logical that I should be able to manipulate any output stream until it's flushed from the buffer.

  4. #4
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    Well, once the output is on the screen, the stream would be flushed, I'd say.

    I can't give you a solution -- never tried what you want to do. File and screen buffers probably have different sizes. I expect the screen buffer to get flushed much oftener than a file buffer.

    Have a look at the timer lib from boost, it has a progress indicator
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  5. #5
    Join Date
    Jun 2004
    Posts
    3
    I had thought it wasn't flushing until I told it to, but it might be.

    Do you know of a command to withhold stream synchronisation until stated otherwise?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured