LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-13-2005, 11:55 PM   #1
ewt3y
Member
 
Registered: May 2005
Location: hanoi vietnam
Distribution: mandriva
Posts: 106

Rep: Reputation: 15
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 ?
 
Old 08-14-2005, 03:40 AM   #2
infinity42
Member
 
Registered: Apr 2005
Location: England
Distribution: Gentoo
Posts: 142

Rep: Reputation: 16
*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.
 
Old 08-14-2005, 04:25 AM   #3
infinity42
Member
 
Registered: Apr 2005
Location: England
Distribution: Gentoo
Posts: 142

Rep: Reputation: 16
Do you just want a unique list of what is in 'EMPLOYEE.DAT'?
 
Old 08-16-2005, 09:48 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,861
Blog Entries: 4

Rep: Reputation: 3995Reputation: 3995Reputation: 3995Reputation: 3995Reputation: 3995Reputation: 3995Reputation: 3995Reputation: 3995Reputation: 3995Reputation: 3995Reputation: 3995
"Go to jail. Go directly to jail. Do not pass GO. Do not collect $200."

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.

... ... ... !!

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux Infinite Reboot Loop SNunweiler Linux - Software 2 05-10-2005 12:31 PM
Java Stopping an Infinite loop oulevon Programming 3 10-18-2004 10:11 PM
infinite loop beginner_84 Programming 5 08-15-2004 02:32 AM
fetchmail stuck in infinite loop Prommy Linux - Software 0 02-17-2004 08:15 AM
Sendmail build - infinite loop?? kstarks Linux - Software 0 10-02-2003 07:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration