not like with printf where you can write one value at a time, but for something like a progress counter with percent done, for example.
In C programming how do you write a changing value to the screen?
Here's a nice console application progress bar for you. Note that the logic/structure would be the same for a windows/GUI app lessay, but different functions (than printf()) would be called for display...so this code is modifiable:
void progress_bar(double cur, double max)
{
#define BAR_LEN 10
char buff[BAR_LEN+2];
double progress = cur/max;
int fill_bars;
/* Avoid divide by zero */
if(!max)
return;
progress = cur/max;
fill_bars = (int)(progress * BAR_LEN);
buff[fill_bars]='\0';
while(fill_bars--)
{
/* I like the looks of ACSII character 178....kind of a filled rectangle....forms a nice bar when in series. You might prefer something else...*/
buff[fill_bars]=(char)178;
}
/* The 'majic' that makes this work: The
carriage return ( \r ) without a line feed....keeps printing to the same line...*/
printf("\r[%-10s]",buff);
}
....call it like so:
#define TRIES (1000L*1000L*1000L)
int main(int argc, char* argv[])
{
long int i:
for(i=0; i%26lt;TRIES; i++)
{
/* We say modulus (i% ) next line, so we don't
call progress_bar() with every iteration....only
every 1000. Else, all this screen output would
slow our program to a crawl... */
if(!(i%10000))
progress_bar(i, TRIES);
}
//Note: some compilers like Watcom may buffer screen output preventing you from seeing anything without a linefeed ( \n ). To solve this, say this in main():
setbuf(stdout, NULL); //make stdout unbuffered
Reply:You would have to keep writing the value to the screen, either as part of a loop, or on interrupt.
If you're programming on a non-windows platform try one of the cross-platform libraries like Allegro (www.allegro.cc or allegro.sourceforge.net) or SDL (sdl.sourceforge.net) which have graphics support, interrupts and a whole lot of extra stuff built-in and ready to run out of the box (also works on windows, but you'll bypasses the windows GUI.
If you want to use the windows GUI then you'd best be using something like either Microsofts C# and downloading libs from them and working out the differences between C and C# (ick!) or try GNU C for Windows (I think it's called MinG32) - try downloading Dev C++ from www.bloodshed.net - a useful IDE which you can download with the compiler all-in-one and comes with libraries to access the windows API.
Probably over-answered your question, but I hope some of this helps!
Cheers!
Reply:sprintf(screendata, "%d x %d, %s ", xpixels, ypixels, colordata);
whatsize = 30;
ExtTextOut(hdc, incr, ym / 2, /* x and y coordinates */
ETO_CLIPPED, /* clips text to rectangle */
%26amp;rectA, /* address of RECT structure */
screendata, /* string to write */
whatsize, /* characters in string */
(LPINT) NULL); /* default character spacing */
You can also look at C Help for “Progress Bar”. This is used in one of my programs with source code in:
http://freespace.virgin.net/roy.longbott...
house plants
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment