Sponsored Content
Full Discussion: C progress bar issues
Top Forums Programming C progress bar issues Post 302380616 by Corona688 on Tuesday 15th of December 2009 04:29:52 PM
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
 

9 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

9. 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
All times are GMT -4. The time now is 07:41 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy