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? -
-
const char DONE = '=';
-
const char BLANK = ' ';
-
const unsigned INCREMENT = 3;
-
static char progressB[(100 / INCREMENT ) + INCREMENT ] = "[ ]";
-
-
void progress(unsigned 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");
-
}
-
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!
3 10555
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
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?
Stop using printf() in C++.
When you:
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: -
ostream& endl(ostream& os)
-
{
-
os.put('\n');
-
os.flush();
-
-
}
-
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: scrodchunk |
last post by:
I'm having a weird problem with printing bytes in hex format.
I have a bunch of print statements that are working okay, then using
the identical formatting later in my code I get some thing where the
formatting and type are ignored.
At one spot in my code I'm loading an array with random bytes
int i;
char byte_block;
|
by: teachtiro |
last post by:
Hi,
'C' says \ is the escape character to be used when characters are
to be interpreted in an uncommon sense, e.g. \t usage in printf(),
but for printing % through printf(), i have read that %% should be used.
Wouldn't it have been better (from design perspective) if the same
escape character had been used in this case too.
Forgive me for posting without verfying things with any standard
compiler, i don't have the means for now.
|
by: infobahn |
last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
|
by: Joakim Hove |
last post by:
Hello,
I have code which makses use of variables of type size_t. The code is
originally developed on a 32 bit machine, but now it is run on both a
32 bit and a 64 bit machine.
In the code have statements like this:
size_t buffer_size;
printf("Total buffer size: %ud bytes \n",buffer_size);
|
by: jacob navia |
last post by:
Has anyone here any information about how arrays can be
formatted with printf?
I mean something besides the usual formatting of each element
in a loop. I remember that Trio printf had some extensions but
I do not recall exactly if they were meant for arrays.
Thanks
| |
by: manochavishal |
last post by:
Hi,
I have a structure
MAX_ID is 5
/**Structure for Copies*/
typedef struct NodeVideoCopy * NodeVideoCopyPtr ;
typedef struct NodeVideoCopy
{
|
by: arindam.mukerjee |
last post by:
I was running code like:
#include <stdio.h>
int main()
{
printf("%f\n", 9/5);
return 0;
}
|
by: sabya123 |
last post by:
I have got a problem using printf and wprintf.
Can I use them simultaneously in a single program?
My problem is that, If I am using both of them in a program only the first one is being taken.
Suppose the first output statement is:-
printf("%s", "Hello world");
then throug out the program only 'printf's are being printed. and the 'wprintf's are not printing anything.
And the reverse is also true, i,e, if the first output statement is...
|
by: yashwant pinge |
last post by:
The following statement
printf("%d %c");
gives the garbage value
Why it is...?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |