I am writing a program to divide my entire database (which comprises of words(A-Z) and phonemic definitions of the corresponding word on the same line) into small text files that become manageable if they are to be read by an 8-bit microcontrolling unit. The database is sorted.

Right now at this stage I am only counting the characters in the smaller sub-divisions of the database. The strategy that I have used to divide my database is first I read the first character of the database and identify the character read after that I read the second character and group them or classify them into one array entry i.e. my array contains the number of characters in AA, AB,AC....BA,BB,BC........ZA,ZY,ZZ, a total of 676 values (26*26).

The problem I am facing is that the output is just not right and I have verified it using a Hex editor.

I am attaching the a ver small sample of the database. The entire database can be found here: https://cmusphinx.svn.sourceforge.net/svnroot/cmusphinx/trunk/cmudict/

#include<stdio.h>
#include<stdlib.h>
int main(){
FILE *fp;
fp=fopen("database2.txt","r");          //Error checking
if(fp==NULL)
{printf("Error opening file!");
exit(1);
}  
 int arr[676], ch='A', ch1='A', count=0, i=0, j=0, k=0, l=0; /* array of 26*26 entries for AA..ZZ, ch and ch1 for           
                                                            reading the first 2 characters of the file,
                                                             count variable to count
                                                            the characters and other variable that serve as indices
                                                            for looping.*/
char a='A', a1='A';                                 // 2 char variables for comparing the first 2 characters of
                                                    // any word                                             
for(i;i<676;i++)                                         //initializing array elements to 0...
arr[i]=0;
i=0;

while((ch=fgetc(fp))!=EOF&&a<='Z')                           /*read first character & check for EOF, 
                                                            loop until a is within the A-Z boundary  
                                                                           */ 
{

if(ch==a)                                                   //is 1st character=a?
{

ch1=fgetc(fp);                                              //read 2nd character if 1st comparison is passed.

    if(ch1==a1)                                             
    {
        count+=2;                           //increment count to 2 because we have already read 2 chars
        while(ch1!='\n'&&ch1!=EOF)          //increment until new line is not reached, store in array
        {ch1=fgetc(fp);
        count++;
        arr[i]=count;
        }
    }


        else                    //ch1!=a1
        {
        if(ch1>='A'&&ch1<='Z')   //enter block only if ch1 is a valid alphabet
        {
        if(ch1>a1)              // ch1 is greater than a1 because the database is sorted
        {

        for(j=0;j<(ch1-a1);j++)  //ch1 is subtracted from a1 to
        i++;                    //increment the index to store in appropriate position.
        count=0;
        count+=2;               //make count=0 as we have read a word belonging to a new entry
        a1=ch1;     //set the variable a1 such that on the next iteration control passes to the if(ch1==a1) block 
        while(ch1!='\n'&&ch1!=EOF)  //as we have read 2 chars of a new word, count upto /n and store    
        {ch1=fgetc(fp);
        count++;
        arr[i]=count;
        }


        }
    }
        else            //if in the database we encounter A..Z and some special character then we skip eg A'.. or A. etc
        {
        count=0;
        while(ch1!='\n'&&ch1!=EOF)
        ch1=fgetc(fp);  
        }


    }





}

else //if the 1st character of the word is not equal to the current variable a, then we increment a 
{        // database contains entries ranging from A to Z, no alphabet is missed out,
        // hence we can increment a to point to next character  

    ++a;
    count=0; //initialize count to 0 as we are going to fill a new entry in array.
    ch1=fgetc(fp); //get 2nd character of the word
    if(ch1>='A'&&ch1<='Z')   //2nd char should be between A-Z.//
    {

    if(ch1>a1||ch1==a1)     // the 2nd char is greater than the current variable a1 or ch==a1
    {for(k=0;k<(26+(ch1-a1));k++) //wrap around! eg if previous 2 chars were AB... and the current chars are BC... or BB...
    i++;                            //then increment the pointer to the appropriate position
    a1=ch1;
    count+=2;   
    while(ch1!='\n'&&ch1!=EOF)      //read the first 2 chars, read the entire word...
    {ch1=fgetc(fp);
    count++;    
    arr[i]=count;   
    }   

}
    else { //if 2nd char is less than variable a1 eg. previous words' starting letters are AY 
    for(l=0;l<(('Z'-a1)+(ch1-'A')+1);l++) //and current words' starting letters are BC 
    i++;
    a1=ch1;
    count+=2;   
    while(ch1!='\n'&&ch1!=EOF)      //read the first 2 chars, read the entire word...
    {ch1=fgetc(fp);
    count++;    
    arr[i]=count;   
    }   




}

}

else
        {

        while(ch1!='\n'&&ch1!=EOF)      //valid 1st char and invalid 2nd char, skip...
        ch1=fgetc(fp);  
        }

}


}



for(j=0;j<676;j++)       //print
{
printf("%d ",arr[j]);
if(j%10==0)
printf("\n");
}

fclose(fp);

    return 0;
}
Attachments
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
ABSENT	AE B S AH N T
ABSENTEE	AE B S AH N T IY
ABSENTEEISM	AE B S AH N T IY IH Z AH M
ABSENTEES	AE B S AH N T IY Z
ABSENTIA	AE B S EH N SH AH
ABSHER	AE B SH ER
ABSHIER	AE B SH IY ER
ABSHIRE	AE B SH AY R
ABSINTHE	AE B S IH N TH
ABSO	AE B S OW
ABSOLOM	AE B S AH L AH M
ABSOLUT	AE B S AH L UW T
ABSOLUT'S	AE B S AH L UW T S
ABSOLUTE	AE B S AH L UW T
ABSOLUTELY	AE B S AH L UW T L IY
ABSOLUTENESS	AE B S AH L UW T N AH S
ABSOLUTES	AE B S AH L UW T S
ABSOLUTION	AE B S AH L UW SH AH N
ABSOLUTISM	AE B S AH L UW T IH Z AH M
ABSOLUTIST	AE B S IH L UW T IH S T
ABSOLVE	AH B Z AA L V
ABSOLVE(2)	AE B Z AA L V
ABSOLVED	AH B Z AA L V D
ABSOLVED(2)	AE B Z AA L V D
ABSOLVES	AH B Z AA L V Z
ABSOLVES(2)	AE B Z AA L V Z
ABSOLVING	AH B Z AA L V IH NG
ABSOLVING(2)	AE B Z AA L V IH NG
ABSORB	AH B Z AO R B
ABSORBED	AH B Z AO R B D
ABSORBENCY	AH B Z AO R B AH N S IY
ABSORBENT	AH B Z AO R B AH N T
ABSORBER	AH B Z AO R B ER
ABSORBERS	AH B Z AO R B ER Z
ABSORBING	AH B Z AO R B IH NG
ABSORBS	AH B Z AO R B Z
ABSORPTION	AH B Z AO R P SH AH N
ABSORPTION(2)	AH B S AO R P SH AH N
ABSTAIN	AH B S T EY N
ABSTAIN(2)	AE B S T EY N
ABSTAINED	AH B S T EY N D
ABSTAINED(2)	AE B S T EY N D
ABSTAINING	AH B S T EY N IH NG
ABSTAINING(2)	AE B S T EY N IH NG
ABSTENTION	AH B S T EH N CH AH N
ABSTENTION(2)	AE B S T EH N CH AH N
ABSTENTIONS	AH B S T EH N CH AH N Z
ABSTENTIONS(2)	AE B S T EH N CH AH N Z
ABSTINENCE	AE B S T AH N AH N S
ABSTINENT	AE B S T AH N AH N T
ABSTON	AE B S T AH N
ABSTRACT	AE B S T R AE K T
ABSTRACTED	AE B S T R AE K T IH D
ABSTRACTION	AE B S T R AE K SH AH N
ABSTRACTIONS	AE B S T R AE K SH AH N Z
ABSTRACTS	AE B S T R AE K T S
ABSTRU

There is no worse description of a probem than "it's just not right"! :(

Give a specific description and an example of the problem.

"Just not right" ---> WTF!

Hey thanks for your time and effort. Sorry, I didn't post the exact descrption of the problem. Anyways I figured it out. Many thanks!

This question has already been answered. Start a new discussion instead.