C progress bar issues


 
Thread Tools Search this Thread
Top Forums Programming C progress bar issues
# 1  
Old 12-12-2009
C progress bar issues

-First-

Hi guys im trying to create a small C app that'll run PING/NETSTAT and such and generate a report... I want to create a progress bar so I figure since I was gonna use multiple commands I was better of to create a function and call the bar when needed to print on the command line

My problem is I cannot get the bar to print on the same line as PING/NETSTAT it always overlap/overright it due to the \r how can I get it to display as below?!?

PING [==========] 100% [COMPLETED]
NETSTAT [=====] 50%

For test purpose in below's code I couldnt get it to work so i put the bar on a newline... but How can I get it on the same line??? thanks in advance...


---------------------------------------------------------------
-Second-

Also where does the (10) comes from??? whenever I run it I get the following... I can't find why it overflow's there... ?

PING
[==========]100% [COMPLETED]
10NETSTAT
[==========]100% [COMPLETED]
10




----------------------Here's what I have so far-------------------------

Code:
#include <stdio.h>
 
int bar();
 
main()
{
printf("PING");
printf("%d", bar());
printf("NETSTAT");
printf("%d", bar());
}
 
bar()
{
int i,j,k,z;
 
printf("\n");
for(i=0;i<=10;i++)
{
printf("[");
for (j=0;j<i;j++)
printf("=");
for (k=j;k<10;k++)
printf("");
printf("]");
z = (i * 10);
printf("%3d%%", z);
if(z == 100)
{
printf(" [COMPLETED]");
}
printf("\r");
fflush(stdout);
usleep(59000);
}
printf("\n");
}


Last edited by Neo; 12-12-2009 at 06:35 AM.. Reason: code tags added
# 2  
Old 12-12-2009
Is there any reason that you need to have a c program? Maybe you can adapt this.. https://www.unix.com/shell-programmin...#post302353830
# 3  
Old 12-12-2009
Yes well I work as a tech support, and one very time consumming issue is having customer that just bought MAC machines or PC with Ubuntu on. That are very clueless about the OS so we have to guide them to the terminals.. then "okay type N... E...T no T as in TANGO..." get the picture lol... so I figured having a simple C program they can click on that does the commands then and export the outputs to a txt file all they would need to have to do is email it to us... but im also thinking of trying to fit in something that would automatically mail the file to us... Im thinking of a couple thing like system()/sendmail option but also I found an interesting way to do it simply through telnet with a bash script so im thinking of adapting it for my program... the best would be 1 execute that would do it all...more simple for less "technologicaly inclined" customers...

Last edited by Jess83; 12-12-2009 at 11:40 PM.. Reason: added infos
# 4  
Old 12-14-2009
You want to change
Code:
printf("\n");

to
Code:
printf("                                   ");

where the number of spaces is appropriate to the length of the message.
# 5  
Old 12-14-2009
I'd also add that the OSX GUI shell won't execute valid script files from a mouseclick, but will happily do the right thing for executables.

There are of course issues with executables too... For starters, if any of your customers have older MAC computers they may have PPC architecture, not x86! An executable can accomodate both if you compile it as a "fat" executable but the procedure for that has eluded me.
# 6  
Old 12-15-2009
Thanks Corona688, good stuff to know. We only support OSX and up nothing less... Lucky though cuz i never thought bout the powerpc/x86 architechture issue when i started this side project lol. I did fix my problem with the (10) that appear out of thin air... i used %d in the printf, thats what caused it... using only

printf("%", bar()); fixed it but anyone knows why???

if im declaring bar() to be a "int" shouldnt it be using %d in printf ???

fixed my overlap problem too... i created seperate functions for each tools less compact but it works...

---------- Post updated at 04:18 AM ---------- Previous update was at 01:56 AM ----------

Anyone ever included html stuff inside C program? I need guidance please lets say I want my C program to connect to a webpage and pass the output of a file to the page how could I do that?
# 7  
Old 12-15-2009
I had to indent your code for you to understand what you were doing.
Code:
#include <stdio.h>
 
int bar();
 
main()
{
        printf("PING");
        printf("%d", bar());
        printf("NETSTAT");
        printf("%d", bar());
}
 
bar()
{
        int i,j,k,z;
 
        printf("\n");
        for(i=0;i<=10;i++)
        {
                printf("[");
                for (j=0;j<i;j++)
                        printf("=");

                for (k=j;k<10;k++)
                        printf("");

                printf("]");

                z = (i * 10);

                printf("%3d%%", z);

                if(z == 100)
                {
                        printf(" [COMPLETED]");
                }

                printf("\r");
                fflush(stdout);
                usleep(59000);
        }
        printf("\n");
}

See the lines in red. That's where the extra numbers are coming from -- the extra print statement to print the extra numbers. Changing it from %d to % stops it happening because you're feeding printf a weird enough command string to make it not "work". It'd be better to just not use printf at all, since you don't want it to print a number there. I'm guessing from how you used it that you're confused as to when printf prints where. The answer is, printf always prints to the same place no matter where you call it, so no extra printf in main is needed there.

Since bar is not returning a value, I've made it void. I've also added a return value and return statement for main, since you really do need one -- without it it returns an unpredictable value. I've had that come back to bite me when scripts using my programs saw the nonzero return, assumed that meant error, and died...

Your k variable is also redundant. You can just reuse it in the second loop instead of adding a brand new var. And your z variable doesn't have much point. Best to minimize the number of variables so you can tell what's going on...

Code:
#include <stdio.h>
 
void bar();
 
int main()
{
        printf("PING");
        bar();
        printf("NETSTAT");
        bar();

        return(0);
}
 
void bar()
{
        int i,j;
 
        printf("\n");
        for(i=0;i<=10;i++)
        {
                printf("[");
                for (j=0;j<i;j++)
                        printf("=");

                for (; j<10;j++)
                        printf(" ");

                printf("]");

                printf("%3d%%", i*10);

                if(i == 10)
                {
                        printf(" [COMPLETED]");
                }

                printf("\r");
                fflush(stdout);
                usleep(59000);
        }
        printf("\n");
}

As for opening an HTML file, that's not really HTML specific. The answer is, you run the program that handles the file. Or, for complicated enough GUI systems, you can run a command that decides what program to use and open it for you. OSX has this as the open command:
Code:
$ man open
OPEN(1)                   BSD General Commands Manual                  OPEN(1)

NAME
     open -- open files and directories

SYNOPSIS
     open [-a application] file ...

     open [-b bundle_identifier] file ...

     open [-e] file ...

     open [-t] file ...

     open [-f]

DESCRIPTION
     The open command opens a file (or a directory or URL), just as if you had
     double-clicked the file's icon. If no application name is specified, the
     default application as determined via LaunchServices is used to open the
     specified files.

You should be able to use it like
Code:
system("open /path/to/file.html");

[edit] I think I misunderstood you, and you want to interface your program with web pages and not just send your client to them. There's no simple way to do it, as it's not a particularly simple process; you should use a library like libcurl or an external program like wget. I think OSX comes with -lcurl, but doesn't come with wget.

Last edited by Corona688; 12-15-2009 at 05:51 PM.. Reason: fix typo in the fixed source
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Progress bar

Hi Experts; Im in the process of writing a shell script for enabling an IT operations to run archiving.We use netbackup. The script is complete, though there is one bit that i need help on. Im trying to have a progess bar for the procedure.I have gone through the man page of the command in... (5 Replies)
Discussion started by: maverick_here
5 Replies

2. Shell Programming and Scripting

Progress bar for cp

I'm trying to use this code to get a progress bar for cp: "Can you get cp to give a progress bar like wget?" But I'm getting these errors: stat: illegal option -- c usage: stat awk: division by zero input record number 1, file source line number 4 I'm using Mac OS X 10.6... (1 Reply)
Discussion started by: pcwiz
1 Replies

3. Programming

A progress bar in C

Hello, it's me again...:eek: I need to create a progress bar in C, but i have no idea on how to do it. i want it to output something like this: Progress: 58% But i can't get it to work. Could you please post an example progress bar written in ANSI C? Thanks (4 Replies)
Discussion started by: Zykl0n-B
4 Replies

4. Shell Programming and Scripting

progress bar

hi all, in shell script (ksh), how do i write a progress bar ?? i have a script which searches files and while its searching i am currently printing out "." and if it finds what its searching for the script prints out the name of the file e.g .................. firstFile.txt... (2 Replies)
Discussion started by: cesarNZ
2 Replies

5. Shell Programming and Scripting

Progress bar

Hi friends, how can I show a progress bar for any running process in the shell script. For example when I am copying or compressing a file. Thanks. (1 Reply)
Discussion started by: dwiravi
1 Replies

6. UNIX for Advanced & Expert Users

how to have a cp progress bar?

Hi all, This is a reformed post to my earlier ones!!!!!! I would like to know how to include a progress bar while using the cp... I am copying a few huge files from cdrom but am unable to figure out ,how to give a progress bar!!!!! I checked out other sites as well,but the issue here is... (1 Reply)
Discussion started by: wrapster
1 Replies

7. UNIX for Advanced & Expert Users

progress bar

Hi all, I want to print # like that in a progress bar.. For e.g We can notice that during installation ... but,how to do that? Thnx, sakthi. (4 Replies)
Discussion started by: sakthi.abdullah
4 Replies

8. Shell Programming and Scripting

TAR- Progress bar?

hi all, i'm currently working on an SGI IRIX system and a solaris system also. In order to transfer files across these two systems i use a storagetek tapedrive with dat tapes. i use TAR to transfer files to tape, roughly 20gb at a time. i'm looking for a simple script using bourne shell if... (1 Reply)
Discussion started by: dicko44
1 Replies

9. Shell Programming and Scripting

progress bar

i am trying to write a script where in it will connect to remote servers and execute remote scripts to fetch some data and ftp it back to a main server. i would like to add a script where it will show some sort of status bar until such time that the expected files have been recieved. something... (3 Replies)
Discussion started by: inquirer
3 Replies
Login or Register to Ask a Question