Thread: loading bar

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    230

    loading bar

    Hello,

    can i add a loading bar to my program? Can anyone help me? It will be better in C++ but if it is possible here i will apreciate this!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	int counter = 0;
    	char buf[100];
    	char cur_word[100];
    	
    	FILE *infile0;
    	FILE *infile1;
    	FILE *infile2;
    	FILE *infile3;
    	FILE *infile4;
    	FILE *infile5;
    	FILE *infile6;
    	FILE *infile7;
    	FILE *infile8;
    
    		infile0 = fopen("f0.txt","r");
    		infile1 = fopen("f1.txt","r");
    		infile2 = fopen("f2.txt","r");
    		infile3 = fopen("f3.txt","r");
    		infile4 = fopen("f4.txt","r");
    		infile5 = fopen("f5.txt","r");
    		infile6 = fopen("f6.txt","r");
    		infile7 = fopen("f7.txt","r");
    		infile8 = fopen("f8.txt","r");
    		
    		if(infile0 == NULL){
    			printf("Error openring infile0. Now exit!\n");
    			exit(1);
    		}else if(infile1 == NULL){
    			printf("Error openring infile1. Now exit!\n");
    			exit(1);
    		}else if(infile2 == NULL){
    			printf("Error openring infile2. Now exit!\n");
    			exit(1);
    		}else if(infile3 == NULL){
    			printf("Error openring infile3. Now exit!\n");
    			exit(1);
    		}else if(infile4 == NULL){
    			printf("Error openring infile4. Now exit!\n");
    			exit(1);
    		}else if(infile5 == NULL){
    			printf("Error openring infile5. Now exit!\n");
    			exit(1);
    		}else if(infile6 == NULL){
    			printf("Error openring infile6. Now exit!\n");
    			exit(1);
    		}else if(infile7 == NULL){
    			printf("Error openring infile7. Now exit!\n");
    			exit(1);
    		}else if(infile8 == NULL){
    			printf("Error openring infile8. Now exit!\n");
    			exit(1);
    		}
    		
    		printf("Type a word to search: ");
    		scanf("%s", cur_word);
    		
    		puts("Searching in the f0.txt...");
    		while(fscanf(infile0, "%s", buf) != EOF){
    			
    			if(strcmp(buf, cur_word) == 0){
    				counter++;
    			}
    		}
    		
    		puts("Searching in the f1.txt...");
    		while(fscanf(infile1, "%s", buf) != EOF){
    			
    			if(strcmp(buf, cur_word) == 0){
    				counter++;
    			}
    		}
    		
    		puts("Searching in the f2.txt...");
    		while(fscanf(infile2, "%s", buf) != EOF){
    			
    			if(strcmp(buf, cur_word) == 0){
    				counter++;
    			}
    		}
    		
    		puts("Searching in the f3.txt...");
    		while(fscanf(infile3, "%s", buf) != EOF){
    			
    			if(strcmp(buf, cur_word) == 0){
    				counter++;
    			}
    		}
    		
    		puts("Searching in the f4.txt...");
    		while(fscanf(infile4, "%s", buf) != EOF){
    			
    			if(strcmp(buf, cur_word) == 0){
    				counter++;
    			}
    		}
    		
    		puts("Searching in the f5.txt...");
    		while(fscanf(infile5, "%s", buf) != EOF){
    			
    			if(strcmp(buf, cur_word) == 0){
    				counter++;
    			}
    		}
    		
    		puts("Searching in the f6.txt...");
    		while(fscanf(infile6, "%s", buf) != EOF){
    			
    			if(strcmp(buf, cur_word) == 0){
    				counter++;
    			}
    		}
    		
    		puts("Searching in the f7.txt...");
    		while(fscanf(infile7, "%s", buf) != EOF){
    			
    			if(strcmp(buf, cur_word) == 0){
    				counter++;
    			}
    		}
    		
    		puts("Searching in the f8.txt...");
    		while(fscanf(infile8, "%s", buf) != EOF){
    			
    			if(strcmp(buf, cur_word) == 0){
    				counter++;
    			}
    		}
    		
    		printf("\n\nGlobal Counter is: %d\n", counter);
    		
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Looks like you're looking for a word in any of several files, yes?

    What is a loading bar? I'm unfamiliar with it.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    yes! the loading bar is something like showing how much progress is your search. If the files are big the search should have some sec. So i want something that says x% of search completed. Like installing a game...

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    For a loading bar, you need to calculate the % (approximately) of the work that has been completed.

    Take a simple example, say you have a bar with 10 spaces in it. So for every 10% of the job that gets done, you advance the loading bar completion, by 1 char. In your program, you show searching through 8 files, so for every 12% completion, you want to show another 1 char more completion on your loading bar. (It's actually a bit more, but we're rounding off, since 8 X 12 = 96, not 100).

    Instead of these messages you have showing progress, I'd use a loading bar like the old DOS games or Windows show:

    Code:
              =========Working========
              [▒▒▒▒▒▒▒▒▒▒40%          ]
    You can use %c with 176, 177, (shown above) or 178 to get a nice looking completion indicator. Use some nice yellow color for the 177 char, on a blue background, and you'll be looking pretty sharp, imo.

    Center the bar in the middle of the (horizontal) screen, and it looks even better.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Of course to do any of that you're going to have to use something other than standard C libraries. If you want C++, then you should (a) not be on this forum, (b) be using different IO functions.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by quzah View Post
    Of course to do any of that you're going to have to use something other than standard C libraries. If you want C++, then you should (a) not be on this forum, (b) be using different IO functions.


    Quzah.
    What problems are you referring to?

    I didn't notice any problems, but the progress bar didn't perfectly match the actual percent of the bytes that had been downloaded, either. Just a rough guide I believe, is enough.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Adak View Post
    Use some nice yellow color for the 177 char, on a blue background, and you'll be looking pretty sharp, imo.

    Center the bar in the middle of the (horizontal) screen, and it looks even better.
    That. There are no colors in standard C.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by quzah View Post
    That. There are no colors in standard C.
    Quzah.
    Ok! All the C standard purists will have to now tear the cones that let you see color, out of your eyeballs! No color for you!

    We want to keep you "pure".

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This isn't a forum for me and my eye cones. It's about C. There's nothing inaccurate about anything I said there. I said that if you do what you said in your post, that you need non-standard libraries to do it. There's nothing wrong with non-standard libraries, I was just pointing out that it is what you need to do the job.

    Not all of us here still use Turbo C, Adak! (Even if we did, we'd still be using non-standard functions to do what you suggested.)


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Every major PC OS has the same functionality that conio.h and curses has, because they need it. DOS, OSX, Linux, OS2, Windows - they all have the ability to display color from a C program.

    Saying that it can't be done using standard C, is just confusing to a beginner.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I didn't say it couldn't be done in C. I said you need non-standard libraries. What part of that confuses you?

    C doesn't know anything about "every major PC OS". The language doesn't care what you run it on. Which is my whole point. If you want color, you need a specific library to handle that for you. Stop pretending you don't.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    Every major PC OS has the same functionality that conio.h and curses has, because they need it. DOS, OSX, Linux, OS2, Windows - they all have the ability to display color from a C program.

    Saying that it can't be done using standard C, is just confusing to a beginner.
    Hi Adak.... Open PellesC, click help -> contents

    Now go to private includes and click on conio.h ... What does it say?

    Now as it works out I happen to agree with you... nothing wrong with cursor positioning and colored text on matte backgrounds. In fact that brilliant red error message can be quite helpful... But for some entirely silly reason it's not part of C99...

    For me, standard or not, it's all just weapons for the washing machine....

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by quzah View Post
    I didn't say it couldn't be done in C. I said you need non-standard libraries. What part of that confuses you?

    C doesn't know anything about "every major PC OS". The language doesn't care what you run it on. Which is my whole point. If you want color, you need a specific library to handle that for you. Stop pretending you don't.


    Quzah.
    I never said you said that it couldn't be done. I said you said that it couldn't be done in standard C, and that is confusing to a raw beginner.

    @Tater:
    I know about conio.h and it being included in Pelles C. My point with Quzah is that it's confusing for him to post that something can't be done in standard C, right after I post how it can be done, in C. Poorly timed, and confusing to a noob, They haven't learned about the standard functions yet, and the thread isn't about the C standard.
    Last edited by Adak; 03-14-2011 at 10:44 PM.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Adak View Post
    I never said you said that it couldn't be done. I said you said that it couldn't be done in standard C, and that is confusing to a raw beginner.
    It doesn't matter if it's confusing or not. It's the truth. It's not any more confusing than "go make that yellow and blue". There is no standard way to make something a color, so telling him to do so is more confusing than me telling him he can't with the standard library functions.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    People learn by studying and practicing - not by being confused. Yes, it does matter.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cargo loading system
    By kyle rull in forum C Programming
    Replies: 1
    Last Post: 04-20-2009, 12:16 PM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. beach bar (sims type game)
    By DrKillPatient in forum Game Programming
    Replies: 1
    Last Post: 03-06-2006, 01:32 PM
  4. progress bar newbie
    By WaterNut in forum Windows Programming
    Replies: 18
    Last Post: 08-09-2004, 01:42 PM
  5. Loading Screen
    By pinkcheese in forum Windows Programming
    Replies: 2
    Last Post: 04-06-2002, 11:48 PM