printf not printing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • IanWright
    New Member
    • Jan 2008
    • 179

    printf not printing

    I'm having a little trouble with a progress bar that I've got in C++. This works fine in Windows but is not working correctly in Linux, and I'm wondering if anyone can help?

    [CODE="cpp"]

    const char DONE = '=';
    const char BLANK = ' ';
    const unsigned INCREMENT = 3;
    static char progressB[(100 / INCREMENT ) + INCREMENT ] = "[ ]";

    void progress(unsign ed int current, char* msg)
    {
    // Work out how many blocks are empty and full
    unsigned int blocks = current / INCREMENT;
    unsigned int remaining = 100 / INCREMENT;
    unsigned int i = 0;

    // All the 'DONE' blocks on progress bar
    while( i < blocks)
    progressB[++i] = DONE;

    // All the 'TO-DO' blocks on progress bar
    while(i < remaining)
    progressB[++i] = BLANK;

    // We print the percentage before the progress bar, but we
    // don't want the bar jigging around as the number of digits
    // change so adjust the number of spaces we use.
    if (current < 10)
    printf(" %u%% ", current);
    else if (current == 100)
    printf(" %u%% ", current);
    else
    printf(" %u%% ", current);

    // Print the progress bar
    printf( "%30s", progressB);
    // Print the message (e.g. What action are we doing?)
    printf(msg);
    // Carriage return, so we can re-print the progress bar next time around
    printf("\r");
    }
    [/CODE]

    The output in windows gives a nice bar filling up with = signs. In Linux I don't get any output whatsoever... unless.

    If I add "cout << endl;" before or after this function gets called then it starts printing out the progress bar. If I use cout without an std::endl then I still get no output. Obviously introducing the endl is bad however because my progress bar then looks like so:

    0% [ ]Import File
    9% [=== ] Importing File
    18% [====== ] Importing File

    On seperate lines. Any help to figure out why this is happening would be much appreciated.

    Thanks!
  • IanWright
    New Member
    • Jan 2008
    • 179

    #2
    I always seem to answer my own question on here... Search for a solution, run out of ideas, make a post, then find the problem!

    I've basically discovered that unlike windows, linux doesn't seem to flush the output to screen very well. I tried a larger sample and wrote just a little progress bar loop, and found that windows would update 100x (1 for each percentage), yet linux would update about 4x (35%, 57%, 73%, 99%) as an example.

    Having a read, I discovered that endl also flushes the output. So I tried

    [CODE=cpp]
    flush(cout);
    [/CODE]

    And it now updates and actually draws on screen regularly! Maybe someone with a bit more expertise could explain why this is the case? And why windows and Linux doesn't seem to behave the same way with the same code?

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Stop using printf() in C++.

      When you:
      [code=c]

      os << data;

      [/code]

      The os can be any ostream: stdout, a diosc file, the network. If you are using the insertion operator your code can direct it's output to any output stream. printf() can't. You have to chnage the code and uses fprintf(), etc. Plus this turkey can only use the built-in types. That is, no way printf() can display your Date object as 02/22/2008. However, a << overload in your Date class can.

      Windows doesn't flush the output buffer any better than Windows does. endl is a function pointer and if you don't use the endl in Windows your buffer may not be flushed either.

      endl looks like:
      [code=cpp]
      ostream& endl(ostream& os)
      {
      os.put('\n');
      os.flush();

      }
      [/code]

      Comment

      • IanWright
        New Member
        • Jan 2008
        • 179

        #4
        weaknessforcats ,

        Thanks for the reply. I'll try to stop using printf(). I didn't realise it was deprecated before hand as I was using g++ for compiling and using previous examples I have lying around. I'll endeavour not to use it in the future.

        Ian

        Comment

        Working...