C program to display a busy symbol while processing


 
Thread Tools Search this Thread
Top Forums Programming C program to display a busy symbol while processing
# 1  
Old 05-03-2012
C program to display a busy symbol while processing

Hi

I was wondering how can a c program will be implemented which will display a symbol while calculating something.

for example : program should display some charters like /\/\ while calculating.

At least provide some pointers

thanks
# 2  
Old 05-03-2012
Here's an extremely simple way:

Code:
#include <stdio.h>
#include <signal.h>

void showspin(void)     // Shows a different charcter every time its called
{
        static char *spin="-/|\\", pos=0;
        if(!spin[++pos]) pos=0; // If we hit the NULL terminator, skip to beginning of string
        // We have to use stderr, so it doesn't buffer, since we're not printing a newline
        fprintf(stderr,"%c\r",spin[pos]); // Show character at position 'pos' in string
}

void dospin(int sig)    // Starts showing spinner repeatedly
{
        showspin();
        signal(SIGALRM, dospin); // Register this function to be called on SIGALRM
        alarm(1); // Cause SIGALRM to happen 1 second later
}

void nospin()           // Cancels the spinner
{
        alarm(0);
        fprintf(stderr," \r"); // Erase the spinner
}

int main(void)
{
        int n;
        dospin(SIGALRM);
        for(n=1; n<(1000*1000*2000); n++);
        nospin();
}

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 05-04-2012
Quote:
Originally Posted by Corona688
Here's an extremely simple way:

Code:
        static char *spin="-/|\\", pos=0;

Hi Corona688, why the variable 'pos' needs to be a char data type? If I change it to int data type then the printing is not happening as expected, it stucks there in '/' char in the screen.
# 4  
Old 05-04-2012
Quote:
Originally Posted by royalibrahim
Hi Corona688, why the variable 'pos' needs to be a char data type?
It doesn't.

I think you didn't make your int static; static locals act like globals, they only get initialized once and keep their values between different function calls. Without static, the variable would be brand-new every time, forgetting its value and starting over at zero.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 05-05-2012
Quote:
Originally Posted by Corona688
It doesn't.

I think you didn't make your int static; static locals act like globals, they only get initialized once and keep their values between different function calls. Without static, the variable would be brand-new every time, forgetting its value and starting over at zero.
Yes Corona688, you're right Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Output Display in a perl program

Hi All, I have created a sample perl program in one of the unix environment as below #!/usr/bin/perl print "Content-type: text/html\n\n"; print "<H1>Hello World</H1>"; When I execute it in unix, I get the below Content-type: text/html <H1>Hello World</H1> However, when I... (1 Reply)
Discussion started by: mr_manii
1 Replies

2. Programming

To display unique lines in a Program

I am new to C programming. I have been trying to display unique lines in a file from past two days. The problem is here, I have a file whose contents are, ras.beamtele.net ras.beamtele.net ras.beamtele.net ras.beamtele.net ras.beamtele.net ras.beamtele.net ras.beamtele.net... (1 Reply)
Discussion started by: Teju88
1 Replies

3. Solaris

/usr/lib/passwdutil.so.1: symbol __nsl_fgetspent_r: referenced symbol not found

deleteing post (0 Replies)
Discussion started by: dshakey
0 Replies

4. Programming

Symbol table of a C program

Hi, is there any command to see symbol table info. will it show where its allocating memory for varibales golbals & locals and code.(i mean the segments). i read there is a section called read only data segment and this is where initialized data such as strings stores. i have wriiten the... (7 Replies)
Discussion started by: MrUser
7 Replies

5. UNIX for Advanced & Expert Users

Filesystem mystery: disks are not busy on one machine, very busy on a similar box

Hi, We have a filesystem mystery on our hands. Given: 2 machines, A and Aa. Machine Aa is the problem machine. Machine A is running Ubuntu, kernel 2.6.22.9 #1 SMP Wed Feb 20 08:46:16 CST 2008 x86_64 GNU/Linux. Machine Aa is running RHEL5.3, kernel 2.6.18-128.el5 #1 SMP Wed Dec 17 11:41:38... (2 Replies)
Discussion started by: mschwage
2 Replies

6. Programming

Reading ELF file Symbol table of C++ program

Folks, I have some program(Test.cpp) as follows, #include<iostream> class Abc { private: int _theVar; public : int printVar(); }; int Abc :: printVar() { _theVar=10; } main() { Abc _t; (2 Replies)
Discussion started by: vinod_chitrali
2 Replies

7. Linux

Reading ELF file Symbol table of C++ program

Folks, I have some program(Test.cpp) as follows, #include<iostream> class Abc { private: int _theVar; public : int printVar(); }; int Abc :: printVar() { _theVar=10; } main() { Abc _t; (0 Replies)
Discussion started by: vinod_chitrali
0 Replies

8. UNIX for Dummies Questions & Answers

Parallel processing using AWK program

Hi All, could any expert tell me about writing a parallel processing program using AWK program is possible or not? Any example would be much appreciated... With Regards / Mysore Ganapati :confused: (5 Replies)
Discussion started by: ganapati
5 Replies

9. Shell Programming and Scripting

Display a Graphic from a Unix program on a Pc how?

An interesting puzzle. I run character based compiled C-Programs in a Unix environment on PCs in a Window. I want to be able to call up and display in a separate window a picture of a product called by a Unix shell script from within my Unix program. Ideally I would like to have a script that... (4 Replies)
Discussion started by: Barry Staples
4 Replies
Login or Register to Ask a Question

Featured Tech Videos