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;
}