Infinite Loop when searching through a database

 
0
 

I have to divide a database and I wanted the statistics of each subdirectory that I am going to create. The database is a dictionary comprising of words and their corresponding phonemic translations. https://cmusphinx.svn.sourceforge.net/svnroot/cmusphinx/trunk/cmudict/sphinxdict/cmudict_SPHINX_40 the database can be found here. I have written a program to count the number of bytes in a subdirectory say the number of bytes in A,B etc. I have not considered the special characters as I am not considering them at the moment. I don't know why my program is getting stuck in an infinite loop!?

/* program used to determine the number of characters and in turn the number of bytes in an
 alphabet entry i.e. number of bytes in 'A', 'B' etc..
 the program gets stuck in an infinite loop and I don't know why.
*/
#include<stdio.h>
#include<stdlib.h>
int main(){
FILE *fp;
fp=fopen("database.txt","rt");
if(fp==NULL)
{printf("Error opening file!");
exit(1);
}                           // File open and error checking
char ch;
ch= fgetc(fp);              
char alphabet='A';
unsigned long countal[26];  //to store the number of bytes for a particular entry (dictionary is sorted)
short i=0;
while(alphabet<='Z')     // A through Z, looping through the entire file untile eof.
{
unsigned long chars=0;
if(ch==alphabet)            /* if found then increment the number of bytes and check the size
{                              of a given entry */
while(ch!='\n')             // infinite loop??
{chars++;
 ch=fgetc(fp);
}
}
else
{
while(ch!='\n')
{
ch=fgetc(fp);
}
}
ch=fgetc(fp);
countal[i]=chars;
 if(ch==EOF)
{i++;
alphabet++;
rewind(fp);
}
}
char abcd='A';
for(i=0;i<26;i++)
{
printf("%c= ",abcd);
printf("%u bytes\n",countal[i]);
abcd++;
}
fclose(fp);
return 0;
}
 
0
 

For starters, if you're ever going to expect to compare ch against EOF, then you must declare ch as int. You'll notice that all of the character I/O functions work with int, and the reason for that is EOF.

 
0
 

Does database.txt use '\n' as end of line characters?
Does database.txt have a '\n' on the last line in the file?

 
0
 

i tried changing ch to int but it is yet not giving me the desired output. Infinite loop again :(

 
0
 

Please post a small sample of your database file.

edit: And fix the following:

if(ch==alphabet)            /* if found then increment the number of bytes and check the size
{                              of a given entry */

The opening brace is caught up in your comment. I assume you fixed it in the real code, because otherwise it wouldn't compile at all, much less create an infinite loop at runtime.

 
0
 

here is a small sample...you can compile my program and check if it's working properly or getting stucked at runtime. Please do give it a look. Many thanks!!

Attachments database1.txt (141.02 KB)
The attachment preview is chopped off after the first 10 KB. Please download the entire file.
!EXCLAMATION-POINT	EH K S K L AH M EY SH AH N P OY N T
"CLOSE-QUOTE	K L OW Z K W OW T
"DOUBLE-QUOTE	D AH B AH L K W OW T
"END-OF-QUOTE	EH N D AH V K W OW T
"END-QUOTE	EH N D K W OW T
"IN-QUOTES	IH N K W OW T S
"QUOTE	K W OW T
"UNQUOTE	AH N K W OW T
#SHARP-SIGN	SH AA R P S AY N
%PERCENT	P ER S EH N T
&AMPERSAND	AE M P ER S AE N D
'BOUT	B AW T
'CAUSE	K AH Z
'COURSE	K AO R S
'CUSE	K Y UW Z
'EM	AH M
'END-INNER-QUOTE	EH N D IH N ER K W OW T
'END-QUOTE	EH N D K W OW T
'FRISCO	F R IH S K OW
'GAIN	G EH N
'INNER-QUOTE	IH N ER K W OW T
'KAY	K EY
'M	AH M
'N	AH N
'QUOTE	K W OW T
'ROUND	R AW N D
'S	EH S
'SINGLE-QUOTE	S IH NG G AH L K W OW T
'TIL	T IH L
'TIS	T IH Z
'TWAS	T W AH Z
(BEGIN-PARENS	B IH G IH N P ER EH N Z
(IN-PARENTHESES	IH N P ER EH N TH AH S IY Z
(LEFT-PAREN	L EH F T P ER EH N
(OPEN-PARENTHESES	OW P AH N P ER EH N TH AH S IY Z
(PAREN	P ER EH N
(PARENS	P ER EH N Z
(PARENTHESES	P ER EH N TH AH S IY Z
)CLOSE-PAREN	K L OW Z P ER EH N
)CLOSE-PARENTHESES	K L OW Z P ER EH N TH AH S IY Z
)END-PAREN	EH N D P ER EH N
)END-PARENS	EH N D P ER EH N Z
)END-PARENTHESES	EH N D P ER EH N TH AH S IY Z
)END-THE-PAREN	EH N D DH AH P ER EH N
)PAREN	P ER EH N
)PARENS	P ER EH N Z
)RIGHT-PAREN	R AY T P ER EH N
)RIGHT-PAREN(2)	R AY T P EH R AH N
)UN-PARENTHESES	AH N P ER EH N TH AH S IY Z
,COMMA	K AA M AH
-DASH	D AE SH
-HYPHEN	HH AY F AH N
...ELLIPSIS	IH L IH P S IH S
.DECIMAL	D EH S AH M AH L
.DOT	D AA T
.FULL-STOP	F UH L S T AA P
.PERIOD	P IH R IY AH D
.POINT	P OY N T
/SLASH	S L AE SH
:COLON	K OW L AH N
;SEMI-COLON	S EH M IY K OW L AH N
;SEMI-COLON(2)	S EH M IH K OW L AH N
?QUESTION-MARK	K W EH S CH AH N M AA R K
A	AH
A'S	EY Z
A(2)	EY
A.	EY
A.'S	EY Z
A.S	EY Z
A42128	EY F AO R T UW W AH N T UW EY T
AAA	T R IH P AH L EY
AABERG	AA B ER G
AACHEN	AA K AH N
AACHENER	AA K AH N ER
AAKER	AA K ER
AALSETH	AA L S EH TH
AAMODT	AA M AH T
AANCOR	AA N K AO R
AARDEMA	AA R D EH M AH
AARDVARK	AA R D V AA R K
AARON	EH R AH N
AARON'S	EH R AH N Z
AARONS	EH R AH N Z
AARONSON	EH R AH N S AH N
AARONSON'S	EH R AH N S AH N Z
AARONSON'S(2)	AA R AH N S AH N Z
AARONSON(2)	AA R AH N S AH N
AARTI	AA R T IY
AASE	AA S
AASEN	AA S AH N
AB	AE B
AB(2)	EY B IY
ABABA	AH B AA B AH
ABABA(2)	AA B AH B AH
ABACHA	AE B AH K AH
ABACK	AH B AE K
ABACO	AE B AH K OW
ABACUS	AE B AH K AH S
ABAD	AH B AA D
ABADAKA	AH B AE D AH K AH
ABADI	AH B AE D IY
ABADIE	AH B AE D IY
ABAIR	AH B EH R
ABALKIN	AH B AA L K IH N
ABALONE	AE B AH L OW N IY
ABALOS	AA B AA L OW Z
ABANDON	AH B AE N D AH N
ABANDONED	AH B AE N D AH N D
ABANDONING	AH B AE N D AH N IH NG
ABANDONMENT	AH B AE N D AH N M AH N T
ABANDONMENTS	AH B AE N D AH N M AH N T S
ABANDONS	AH B AE N D AH N Z
ABANTO	AH B AE N T OW
ABARCA	AH B AA R K AH
ABARE	AA B AA R IY
ABASCAL	AE B AH S K AH L
ABASH	AH B AE SH
ABASHED	AH B AE SH T
ABASIA	AH B EY ZH Y AH
ABATE	AH B EY T
ABATED	AH B EY T IH D
ABATEMENT	AH B EY T M AH N T
ABATEMENTS	AH B EY T M AH N T S
ABATES	AH B EY T S
ABATING	AH B EY T IH NG
ABBA	AE B AH
ABBADO	AH B AA D OW
ABBAS	AH B AA S
ABBASI	AA B AA S IY
ABBATE	AA B EY T
ABBATIELLO	AA B AA T IY EH L OW
ABBE	AE B IY
ABBE(2)	AE B EY
ABBENHAUS	AE B AH N HH AW S
ABBETT	AH B EH T
ABBEVILLE	AE B V IH L
ABBEY	AE B IY
ABBEY'S	AE B IY Z
ABBIE	AE B IY
ABBITT	AE B IH T
ABBOT	AE B AH T
ABBOTSTOWN	AE B AH T S T AW N
ABBOTT	AE B AH T
ABBOTT'S	AE B AH T S
ABBOTTSTOWN	AE B AH T S T AW N
ABBOUD	AH B UW D
ABBOUD(2)	AH B AW D
ABBREVIATE	AH B R IY V IY EY T
ABBREVIATED	AH B R IY V IY EY T AH D
ABBREVIATED(2)	AH B R IY V IY EY T IH D
ABBREVIATES	AH B R IY V IY EY T S
ABBREVIATING	AH B R IY V IY EY T IH NG
ABBREVIATION	AH B R IY V IY EY SH AH N
ABBREVIATIONS	AH B R IY V IY EY SH AH N Z
ABBRUZZESE	AA B R UW T S EY Z IY
ABBS	AE B Z
ABBY	AE B IY
ABCO	AE B K OW
ABCOTEK	AE B K OW T EH K
ABDALLA	AE B D AE L AH
ABDALLAH	AE B D AE L AH
ABDEL	AE B D EH L
ABDELLA	AE B D EH L AH
ABDICATE	AE B D AH K EY T
ABDICATED	AE B D AH K EY T AH D
ABDICATES	AE B D AH K EY T S
ABDICATING	AE B D IH K EY T IH NG
ABDICATION	AE B D IH K EY SH AH N
ABDNOR	AE B D N ER
ABDO	AE B D OW
ABDOLLAH	AE B D AA L AH
ABDOMEN	AE B D OW M AH N
ABDOMEN(2)	AE B D AH M AH N
ABDOMINAL	AE B D AA M AH N AH L
ABDOMINAL(2)	AH B D AA M AH N AH L
ABDUCT	AE B D AH K T
ABDUCTED	AE B D AH K T IH D
ABDUCTED(2)	AH B D AH K T IH D
ABDUCTEE	AE B D AH K T IY
ABDUCTEES	AE B D AH K T IY Z
ABDUCTING	AE B D AH K T IH NG
ABDUCTING(2)	AH B D AH K T IH NG
ABDUCTION	AE B D AH K SH AH N
ABDUCTION(2)	AH B D AH K SH AH N
ABDUCTIONS	AE B D AH K SH AH N Z
ABDUCTIONS(2)	AH B D AH K SH AH N Z
ABDUCTOR	AE B D AH K T ER
ABDUCTOR(2)	AH B D AH K T ER
ABDUCTORS	AE B D AH K T ER Z
ABDUCTORS(2)	AH B D AH K T ER Z
ABDUCTS	AE B D AH K T S
ABDUL	AE B D UW L
ABDULAZIZ	AE B D UW L AH Z IY Z
ABDULLA	AA B D UW L AH
ABDULLAH	AE B D AH L AH
ABE	EY B
ABED	AH B EH D
ABEDI	AH B EH D IY
ABEE	AH B IY
ABEL	EY B AH L
ABELA	AA B EH L AH
ABELARD	AE B IH L ER D
ABELE	AH B EH L
ABELES	AH B EH L Z
ABELES(2)	EY B AH L IY Z
ABELL	EY B AH L
ABELLA	AH B EH L AH
ABELN	AE B IH L N
ABELOW	AE B AH L OW
ABELS	EY B AH L Z
ABELSON	AE B IH L S AH N
ABEND	AE B EH N D
ABEND(2)	AH B EH N D
ABENDROTH	AE B IH N D R AO TH
ABER	EY B ER
ABERCROMBIE	AE B ER K R AA M B IY
ABERDEEN	AE B ER D IY N
ABERFORD	EY B ER F ER D
ABERG	AE B ER G
ABERLE	AE B ER AH L
ABERLE(2)	AE B ER L
ABERMIN	AE B ER M IH N
ABERNATHY	AE B ER N AE TH IY
ABERNETHY	AE B ER N EH TH IY
ABERRANT	AE B EH R AH N T
ABERRATION	AE B ER EY SH AH N
ABERRATIONAL	AE B ER EY SH AH N AH L
ABERRATIONS	AE B ER EY SH AH N Z
ABERT	AE B ER T
ABET	AH B EH T
ABETTED	AH B EH T IH D
ABETTING	AH B EH T IH NG
ABEX	EY B EH K S
ABEYANCE	AH B EY AH N S
ABEYTA	AA B EY T AH
ABHOR	AE B HH AO R
ABHORRED	AH B HH AO R D
ABHORRENCE	AH B HH AO R AH N S
ABHORRENT	AE B HH AO R AH N T
ABHORS	AH B HH AO R Z
ABIAM	EY B IY AH M
ABIAM'S	EY B IY AH M Z
ABID	EY B IH D
ABIDE	AH B AY D
ABIDED	AH B AY D IH D
ABIDES	AH B AY D Z
ABIDING	AH B AY D IH NG
ABIDJAN	AE B IH JH AA N
ABIE	AE B IY
ABIGAIL	AE B AH G EY L
ABILA	AA B IY L AH
ABILENE	AE B IH L IY N
ABILITIES	AH B IH L AH T IY Z
ABILITY	AH B IH L AH T IY
ABIMAEL	AE B IH M EY L
ABIMAELS	AE B IH M EY L Z
ABINGDON	AE B IH NG D AH N
ABINGTON	AE B IH NG T AH N
ABIO	AA B IY OW
ABIOLA	AA B IY OW L AH
ABIOLA'S	AA B IY OW L AH Z
ABIOMED	EY B IY AH M EH D
ABIQUIU	AH B IH K Y UW
ABITIBI	AE B IH T IY B IY
ABITZ	AE B IH T S
ABJECT	AE B JH EH K T
ABKHAZIA	AE B K AA Z Y AH
ABKHAZIA(2)	AE B K AE Z Y AH
ABKHAZIAN	AE B K AA Z IY AH N
ABKHAZIAN(2)	AE B K AE Z IY AH N
ABKHAZIAN(3)	AE B K AA Z Y AH N
ABKHAZIAN(4)	AE B K AE Z Y AH N
ABKHAZIANS	AE B K AA Z IY AH N Z
ABKHAZIANS(2)	AE B K AE Z IY AH N Z
ABLAZE	AH B L EY Z
ABLE	EY B AH L
ABLE-BODIED	EY B AH L B AA D IY D
ABLED	EY B AH L D
ABLER	EY B AH L ER
ABLER(2)	EY B L ER
ABLES	EY B AH L Z
ABLEST	EY B AH L S T
ABLEST(2)	EY B L AH S T
ABLOOM	AH B L UW M
ABLY	EY B L IY
ABNER	AE B N ER
ABNEY	AE B N IY
ABNORMAL	AE B N AO R M AH L
ABNORMALITIES	AE B N AO R M AE L AH T IY Z
ABNORMALITY	AE B N AO R M AE L AH T IY
ABNORMALLY	AE B N AO R M AH L IY
ABO	AA B OW
ABO'S	AA B OW Z
ABOARD	AH B AO R D
ABODE	AH B OW D
ABOHALIMA	AE B AH HH AH L IY M AH
ABOLISH	AH B AA L IH SH
ABOLISHED	AH B AA L IH SH T
ABOLISHES	AH B AA L IH SH IH Z
ABOLISHING	AH B AA L IH SH IH NG
ABOLITION	AE B AH L IH SH AH N
ABOLITIONISM	AE B AH L IH SH AH N IH Z AH M
ABOLITIONIST	AE B AH L IH SH AH N AH S T
ABOLITIONISTS	AE B AH L IH SH AH N AH S T S
ABOLITIONISTS(2)	AE B AH L IH SH AH N AH S
ABOMINABLE	AH B AA M AH N AH B AH L
ABOMINATION	AH B AA M AH N EY SH AH N
ABOOD	AH B UW D
ABOODI	AH B UW D IY
ABOR	AH B AO R
ABORIGINAL	AE B ER IH JH AH N AH L
ABORIGINE	AE B ER IH JH AH N IY
ABORIGINES	AE B ER IH JH AH N IY Z
ABORN	AH B AO R N
ABORT	AH B AO R T
ABORTED	AH B AO R T IH D
ABORTIFACIENT	AH B AO R T AH F EY SH AH N T
ABORTIFACIENTS	AH B AO R T AH F EY SH AH N T S
ABORTING	AH B AO R T IH NG
ABORTION	AH B AO R SH AH N
ABORTIONIST	AH B AO R SH AH N IH S T
ABORTIONISTS	AH B AO R SH AH N IH S T S
ABORTIONISTS(2)	AH B AO R SH AH N IH S
ABORTIONS	AH B AO R SH AH N Z
ABORTIVE	AH B AO R T IH V
ABORTS	AH B AO R T S
ABOTT	AH B AA T
ABOU	AH B UW
ABOUD	AA B UW D
ABOUHALIMA	AA B UW HH AA L IY M AH
ABOUHALIMA'S	AA B UW HH AA L IY M AH Z
ABOUND	AH B AW N D
ABOUNDED	AH B AW N D IH D
ABOUNDING	AH B AW N D IH NG
ABOUNDS	AH B AW N D Z
ABOUT	AH B AW T
ABOUT'S	AH B AW T S
ABOVE	AH B AH V
ABOVE'S	AH B AH V Z
ABOVEBOARD	AH B AH V B AO R D
ABPLANALP	AE B P L AH N AE L P
ABRA	AA B R AH
ABRACADABRA	AE B R AH K AH D AE B R AH
ABRAHAM	EY B R AH HH AE M
ABRAHAMIAN	AE B R AH HH EY M IY AH N
ABRAHAMS	EY B R AH HH AE M Z
ABRAHAMSEN	AE B R AH HH AE M S AH N
ABRAHAMSON	AH B R AE HH AH M S AH N
ABRAM	AH B R AE M
ABRAM'S	EY B R AH M Z
ABRAMCZYK	AA B R AH M CH IH K
ABRAMO	AA B R AA M OW
ABRAMOVITZ	AH B R AA M AH V IH T S
ABRAMOWICZ	AH B R AA M AH V IH CH
ABRAMOWITZ	AH B R AA M AH W IH T S
ABRAMS	EY B R AH M Z
ABRAMS'S	EY B R AH M Z IH Z
ABRAMSON	EY B R AH M S AH N
ABRASION	AH B R EY ZH AH N
ABRASIONS	AH B R EY ZH AH N Z
ABRASIVE	AH B R EY S IH V
ABRASIVES	AH B R EY S IH V Z
ABRAXA	AH B R AE K S AH
ABRAXA'S	AH B R AE K S AH Z
ABRAXAS	AH B R AE K S AH Z
ABREAST	AH B R EH S T
ABREGO	AA B R EH G OW
ABREU	AH B R UW
ABRIDGE	AH B R IH JH
ABRIDGED	AH B R IH JH D
ABRIDGEMENT	AH B R IH JH M AH N T
ABRIDGES	AH B R IH JH AH Z
ABRIDGING	AH B R IH JH IH NG
ABRIL	AH B R IH L
ABROAD	AH B R AO D
ABROGATE	AE B R AH G EY T
ABROGATED	AE B R AH G EY T IH D
ABROGATING	AE B R AH G EY T IH NG
ABROGATION	AE B R AH G EY SH AH N
ABROL	AH B R OW L
ABRON	AH B R AA N
ABRUPT	AH B R AH P T
ABRUPTLY	AH B R AH P T L IY
ABRUPTNESS	AH B R AH P T N AH S
ABRUTYN	EY B R UW T IH N
ABRUZZESE	AA B R UW T S EY Z IY
ABRUZZO	AA B R UW Z OW
ABS	EY B IY EH S
ABS(2)	AE B Z
ABSALOM	AE B S AH L AH M
ABSARAKA	AE B S AA R AH K AH
ABSCAM	AE B S K AE M
ABSCESS	AE B S EH S
ABSCOND	AE B S K AA N D
ABSCONDED	AE B S K AA N D AH D
ABSCONDING	AE B S K AA N D IH NG
ABSCONDS	AE B S K AA N D Z
ABSECON	AE B S AH K AO N
ABSENCE	AE B S AH N S
ABSENCES	AE B S AH N S IH Z
A
 
0
 

Turn anything that looks like this:

while(ch!='\n')

Into this:

while(ch != EOF && ch!='\n')
 
0
 

Thanks totally worked!! What was the problem????

Question Answered as of 2 Years Ago by deceptikon and Banfa
 
0
 

If the stream is at the end of the file, fgetc() will always return EOF regardless of how many times you call it. In other words, unless you have a breaking condition that checks for EOF or ensure that end-of-file won't be reached in the loop, it will be infinite.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article