LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Infinite Loop (http://www.linuxquestions.org/questions/programming-9/infinite-loop-353032/)

ewt3y 08-13-2005 11:55 PM

Infinite Loop
 
Code:

#include <stdio.h>
#include <string.h>
const char FOUND=0xf;
FILE *f, *g;
char c,t[22],s[22],i=0,j=0,k;

char cmpstr() {
        printf("P");
        g=fopen("dict.dat","r");
        while( feof(g)==0) {
                fscanf(g,"%s",t);
                if( strncmp(t,s,i)==0) {
                        fclose(g);
                        return FOUND;
                }
        }

        fclose(g);
        return 1;
}
main() {
        FILE *h;
        f=fopen("EMPLOYEE.DAT","r");
        h=fopen("a.dat","w");
        while(feof(f)==0) {
                z:;
                c=getc(f);
                x:;
                if( c>64 && c<91) {
                        s[i]=c;
                        i++;
                        goto z;
                }else if( c>96 && c<123 || c=='-'){
                        c-=32;
                        goto x;
                }else{
                        s[i]="\0";
                        if( i != 0 ) {
                                if (cmpstr() != FOUND) {
                        /*                if(j--<1) {
                                                j=3;
                                                clrscr();
                                                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
                                        }
                                        printf("                      ");
                                        for( k=0;k<i;k++) {
                                                printf("%c ",s[k]);
                                        }
                                        printf("\n\n");
                                        c=getch();        */
                                        fprintf(h,"%s\n",s);
                                        g=fopen("dict.dat","a");
                                        fprintf(g,"%s\n",s);
                                        fclose(g);
                                }
                        }

                        i=0;
                }

        }
        fclose(f);
        fclose(h);
}

the while () {} in main() is infinite, can you tell why ? Any optimizations for me ?

infinity42 08-14-2005 03:40 AM

*screams in horror* GOTOs?

Sorry, but I have had a phobia of GOTOs ever since my BASIC programming days scarred me horrifically...

Global variables? *screams again*

Why did you include the big commented out block?

Also looks like there could be some really bad buffer overflows if 'fscanf(g,"%s",t);' returns a string longer than 22 characters.

Optimisations...
If you have the memory store the dictionary in a vector of strings so you won't have to read it from disk every time.

Well I'll have a look at it and play around, as I'm bored.. see if i can actually work out what it is doing. I'll probably post back later.

infinity42 08-14-2005 04:25 AM

Do you just want a unique list of what is in 'EMPLOYEE.DAT'?

sundialsvcs 08-16-2005 09:48 AM

"Go to jail. Go directly to jail. Do not pass GO. Do not collect $200." :mad:

Seriously, this code is unreadable. There's not a line of comments to it, there's no explanation of what the magic numbers (ASCII?) mean, and no consideration of things like multi-byte (international) character-sets.

:eek: ... :eek: ... :eek: ... !!

Code like this simply must be rewritten. Make it clear. When you do so, you'll notice problems like "dict.dat" being opened, closed, and read for every word, and then (without the slightest bit of explanation, of course) opened and closed again in the main routine.

If you submit this kind of code to your computer and its disk-drive, it will quite righteously throw you out. :tisk: I would never allow such incomprehensible stuff to be checked-in to my CVS repository!

I mean this ... actually ... in a serious but friendly way. Hie thee out and buy a copy of The Elements of Programming Style (even the original FORTRAN edition) and read it.

You are not allowed to use your "Get Out Of Jail Free" card until you do! ;)


All times are GMT -5. The time now is 07:58 PM.