Thread: problem in reading alphabets from file

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    91

    problem in reading alphabets from file

    Hello,

    This is a program which creates two arrays and a variable :

    1. one for each alphabet

    Code:
    int char_array[MAXCHAR] = {0};
    (MAXCHAR = 26);

    2. one for each digit

    Code:
    int digit_array[MAXDIGIT] = {0};
    (MAXDIGIT = 9);

    3. variable counter for all other characters

    Code:
    int other_char = 0;
    The program reads a file test1.txt and increments the frequency of each digit or alphabet or character in the assigned array or variable. (AN ASSUMPTION IS MADE THAT ALL CHARACTERS READ ARE LOWER CASE)

    The program works fine when its reading digits or special characters but does not work when its reading alphabets and doesn't enter the condition :

    Code:
    else if (c >= 'a' && c >= 'z')
                      ++char_array[c - 'a'];

    Can anyone help me on this

    The code is listed as follows :

    Code:
    #include<stdio.h>
    
    /*  ******************************************************************************************* */
    /*  This program prints an array of the frequencies of characters in a file input */
    /*  ******************************************************************************************* */
    
    
    /* Constant declarations to define size for arrays containing 26 alphabets and 10 digits */
    
    # define MAXCHAR   26       
    # define MAXDIGIT  10        
    
    
    int main()
    {
        /* variables 'c' stores the character  read from the file */ 
        /* Arrays char_array[] and digit_array[] are declared and elements are initialized to zero */
        /* Arrays char_array[] and digit_array[] store frequency of characters and digits in the file read */
        /* variable other_char stores the frequency of all other characters and is initialized to zero */
        
        int c;
        int char_array[MAXCHAR] = {0};
        int digit_array[MAXDIGIT] = {0};
        int other_char = 0;
        
        /*  Declare a file pointer fp and open the file test1.txt to read the input */
        
        FILE *fp;                         
        fp = fopen("test1.txt", "r+");
        
        
        
        /* start the while loop after that and read the input into 'c' untill the end of file */
        
        while((c = getc(fp)) != EOF)
        {
                 /* If character read is a digit then increase the assigned character frequency by 1 */
                 if (c >= '0' && c <= '9')
                    ++digit_array[c - '0'];
                 
                 /* If character read is an alphabet then increase the assigned character frequency by 1 */
                 
                 else if (c >= 'a' && c >= 'z')
                      ++char_array[c - 'a'];
                 
                 /* character read is special character hence increment special character count */
                  
                 else
                     ++other_char;
        }
        
    /* use a variable counter to print the elements of the arrays declared in the beginning */
        
        int counter;
        for(counter = 0; counter < MAXDIGIT; ++counter)
        {
                    if (digit_array[counter] == 0)
                       continue;
                    
                    else
                        printf("%d\n", digit_array[counter]);
        }
        
        for(counter = 0; counter < MAXCHAR; ++counter)
        {
                        printf("%d", char_array[counter]);
        }
        
        printf ("%d\n", other_char);
        
        fclose(fp);
        return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What exactly do you need help with?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    91
    my program does not read alphabets from a file , it does not enter the loop

    else if (c >= 'a' && c >= 'z')
    ++char_array[c - 'a'];

    can u tell me why ? or give me some hint of what I am doing wrong in the program !!!

  4. #4
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    > else if (c >= 'a' && c >= 'z')

    c >= 'z'

    should be

    c <= 'z'

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    91
    great !!! it works I can't believe I missed a stupid mistake like that !!!!

    Thanks scarlet7 !!!!

    One more question, the program has a drawback when it reads the last character or EOF , it considers it as a special character and this increments the "other_char " counter, how can I avoid that ??

  6. #6
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    If test1.txt has multiple lines then the code will count the new line character '\n' or 0x10 and increment 'other_char'. You need to add some code to check for '\n' and not increment 'other_char'. The EOF logic is ok.

    Code:
    else if(c != '\n')
           ++other_char;

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Simple, just quit the loop when you reach EOF. However, I don't think it's counting EOF, because your loop is set to end on EOF. Therefore, the internals of it should never be reached. Is this the actual loop, or are you using a do-while or something? If you really think it's still trying to count EOF, change your loop:
    Code:
    for( ;; )
    {
        if( (c = fgetc( fp )) != EOF )
        {
            ...your stuff here...
        }
        else
            break;
    }
    Or if you prefer...
    Code:
    for( ;; )
    {
        if( (c = fgetc( fp )) == EOF )
            break;
    
        ...your stuff here...
    }
    But in any case, with what you have, it isn't counting EOF. The loop ends as soon as it is read.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Subtle(?) File I/O Problem
    By cecomp64 in forum C Programming
    Replies: 9
    Last Post: 07-16-2008, 11:39 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Replies: 20
    Last Post: 06-12-2005, 11:53 PM
  5. Problem reading file
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 04-23-2004, 06:52 AM