470,870 Members | 1,049 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,870 developers. It's quick & easy.

printf not printing

179 100+
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?

Expand|Select|Wrap|Line Numbers
  2. const char DONE = '=';                                               
  3. const char BLANK = ' ';
  4. const unsigned INCREMENT = 3;
  5. static char progressB[(100 / INCREMENT ) + INCREMENT ] = "[                                 ]";
  7. void progress(unsigned int current, char* msg)
  8.     // Work out how many blocks are empty and full
  9.     unsigned int blocks = current / INCREMENT;
  10.     unsigned int remaining = 100 / INCREMENT; 
  11.     unsigned int i = 0;
  13.     // All the 'DONE' blocks on progress bar
  14.     while( i < blocks)
  15.         progressB[++i] = DONE;
  17.     // All the 'TO-DO' blocks on progress bar
  18.     while(i < remaining)
  19.         progressB[++i] = BLANK;
  21.     // We print the percentage before the progress bar, but we 
  22.     // don't want the bar jigging around as the number of digits
  23.     // change so adjust the number of spaces we use.
  24.     if (current < 10)
  25.         printf("   %u%% ", current);
  26.     else if (current == 100)
  27.         printf(" %u%% ", current);
  28.     else 
  29.         printf("  %u%% ", current);
  31.     // Print the progress bar
  32.     printf( "%30s", progressB);
  33.     // Print the message (e.g. What action are we doing?)
  34.     printf(msg);
  35.     // Carriage return, so we can re-print the progress bar next time around
  36.     printf("\r");
  37. }
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.

Feb 22 '08 #1
3 10165
179 100+
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

Expand|Select|Wrap|Line Numbers
  1. flush(cout);
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?
Feb 22 '08 #2
9,208 Expert Mod 8TB
Stop using printf() in C++.

When you:
Expand|Select|Wrap|Line Numbers
  2. os << data;
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:
Expand|Select|Wrap|Line Numbers
  1. ostream& endl(ostream& os)
  2. {
  3.        os.put('\n');
  4.        os.flush();
  6. }
Feb 22 '08 #3
179 100+

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.

Feb 26 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by scrodchunk | last post: by
188 posts views Thread by infobahn | last post: by
25 posts views Thread by Joakim Hove | last post: by
27 posts views Thread by jacob navia | last post: by
8 posts views Thread by manochavishal | last post: by
reply views Thread by sabya123 | last post: by
14 posts views Thread by yashwant pinge | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.