View Full Version : progress bars and such...
Colonel Sanders
12-17-2004, 04:21 AM
I was wondering if someone could help me figure out how to write a program which would simply display a "spinning line". By a spinning line I simply mean a character that would change, cycling through |, /, -, \, and back to | to give the illusion that the line is spinning. I know I've seen this on some flavors of Linux.
I only want this to show that the program is still running and not locked up, plus I think it looks cool. =)
Any suggestions?
TIA, L J
Here you go:
#include <unistd.h>
#include <stdio.h>
int main(void) {
char c[4] = "|/-\\";
int i = 0;
while (1) {
printf("\r%c", c[i]);
fflush(stdout);
i = (i + 1) % 4;
sleep(1);
}
return 0;
}
Ctrl+C to break out.
Colonel Sanders
12-17-2004, 10:50 AM
Is the second "\" in a row a typo or necessary, will fflush (stdout) clear the whole screen or just where the character, and how do I position this character at say, the upper right corner of the screen?
Also you say Ctrl + C to break out, what part of the code causes this? I was thinking of doing something like a while loop ( while x==0 ), then pushing a key would change the value of x to exit the program.
L J
No, the second \ is not a typo, \ is the escape character, \n means a new line for example, so you need two \s to actually get one \ .
fflush (stdout) sends the output from the buffer to the screen, it doesn't clear the screen, \r is the one that clears the previous character.
Ctrl+C sends a signal to the program, and the program exits because it doesn't handle the signal, your way to exit can be used too.
Colonel Sanders
12-18-2004, 03:00 PM
I'm pretty sure I have DJGPP configured wrong or something, I have compiled your code and another far less complex (just a little more advanced than a "hello world" app though) and the cursor randomly moves around the screen.
CMD proceeds to crash, than eventually I get an error something about "the file sent to LPT1 cant be printed, no printer attached"...
Any suggestions on how to configure DJGPP for Windows 2000? I am running version 2.03, which the DJGPP website claimed would work with Win2K...
TIA, L J
*any recomendations for a free compiler that will work with Windows 2000?
Hmm, I didn't test that under Windows, I thought you were looking for a Linux program.
For Windows, I suggest you try DevC++: (free)
http://www.bloodshed.net/dev/devcpp.html
Replace unistd.h with windows.h, and sleep(1) with Sleep(100).
Let me know if it works, I'll try to test it to under Windows too.
vBulletin® v3.7.0, Copyright ©2000-2008, Jelsoft Enterprises Ltd.