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-------------------------

#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");
}

Is there any reason that you need to have a c program? Maybe you can adapt this.. http://www.unix.com/shell-programming-scripting/119364-until-loop-fails.html\#post302353830

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...

You want to change

printf("\n");

to

printf("                                   ");

where the number of spaces is appropriate to the length of the message.

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.

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?

I had to indent your code for you to understand what you were doing.

#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...

#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:

$ 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

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.

Yeah thats what I figured with the html option, it would of been useful because it provided me a way to accomplish the task without relying on a MTA for the email to be sent. I'll try scripting something with telnet and get the right timing to pipe in a .txt and see how well it works... unless i can find a very simple and straight forward code and i'll try to get the permission from the autor to use them in my prog. Cuz im foreseing lots of issues with a script to handle emails sent through telnet via smtp...

---------- Post updated at 11:57 PM ---------- Previous update was at 11:43 PM ----------

Thanks alot Corona for the explanation man, gave you some bits... Not too sure what they do but whatever... Thanks man