Thread: count the number occurance from text file for C program

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    2

    count the number occurance from text file for C program

    I’m the beginner to write the C program. Always got the error on output. Anyone can help. The program is open and read the text file. Then count all the number occurrence.
    File format like this
    21 43 32
    32 32 21
    43 5 6…


    The code as below

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE 0
    int main ()
    {
        int number1[SIZE], number2[SIZE];
        int i, j;
        int count;
        int n;
        char filename[40] ;
        FILE *fp;
    printf("Enter file name: ");
        gets( filename );
        fp = fopen( filename, "r" );
        if ( fp == NULL )
        {
            printf("Cannot open %s for reading \n", filename );
            exit(1);      /* terminate program */
    }
       n = fgetc( fp ) ;
        while (  n != EOF )
        {
        for (i=0; i<n; i++)
        {
            count=0;
            for (j=0; j<n; j++)
            {
                if (number1[j] == number1[i])
                {
                    count++;
                    }
                }
            number2[i] = count;
             }
        printf ("\n\n");
        for (i=0; i<n; i++)
           {
            printf ("Number %d: occurance %d times(s)\n", number1[i], number2[i]);
        }
     fclose( fp );
        return 0;
    }
    }

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Why SIZE 0? ???!?!?!?!???

    You cannot have an array with size 0.
    Turn on your compiler warnings and mind the warnings.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Problems to correct before fixing your logic:

    - You've defined "SIZE" to be zero, so your "number1" and "number2" arrays have zero indices.
    - You read a character from the file once, then enter a while loop where 'n' doesn't change. You probably want "while((n=fgetc(fp)) != EOF)".
    - And I don't think you want to be reading characters, you want to read integers. Consider "fscanf()" as one possibility.
    - Don't use "gets()" as it is unsafe - use "fgets()" instead. FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com
    - You show good intentions to use proper indentation, but it gets funky in some spots. As a result, you miss that the "while()" closing brace is in the wrong place (it is at the end of "main()").

    ---

    Now, it appears that this program was written all in one go, compiled, and didn't work. That's not how to do it. Take it one step at a time.

    First, write a program that opens the file, loads numbers into the array, and prints the arrays.

    When you have this working, then begin developing the logic to accomplish the task.

    I recommend reading this: A development process

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    2
    Modified the code as below. Seem it still can't read the file. Anyone can help. Thanks

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE 20
    
    int main ()
    {
        int number1[SIZE], number2[SIZE];
        int i, j;
        int count;
        int n=10;
        FILE *fp;
        
       char filename[40] ;
       printf("Enter file name: ");
        gets( filename );
        fp = fopen( filename, "r" );
        if ( fp == NULL )
        {
            printf("Cannot open %s for reading \n", filename );
            exit(1);      /* terminate program */  
    }
        
       fscanf(fp,"%d",&count);
      
            for (i=0; i<n; i++)
        {
            printf ("%d ", number1[i]);
        }
    
        for (i=0; i<n; i++)
        {
            count=0;
    
            for (j=0; j<n; j++)
            {
                if (number1[j] == number1[i])
                {
                    count++;
                }
            }
    
            number2[i] = count;
        }
    
        printf ("\n\n");
    
        for (i=0; i<n; i++)
        {
            printf ("Number %d: %d occurencese\n", number1[i], number2[i]);
        }
     fclose(fp);
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    How many times are you trying to read data from the file?

    Only once.

    You need to fscanf() inside the loop, as well as the initial fscanf() outside the loop (which you have in place).

    If you aren't going to use "count" after you read it from the file, I think that's probably wrong. <n should be <count shouldn't it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-02-2011, 09:19 PM
  2. Find first occurance of a number in a string.
    By 337higgin in forum C Programming
    Replies: 8
    Last Post: 09-13-2011, 08:04 AM
  3. Count number of lines in a text file
    By eXeCuTeR in forum C# Programming
    Replies: 3
    Last Post: 04-30-2008, 01:46 AM
  4. Count number of letters and numbers in a file
    By jtullo in forum C Programming
    Replies: 2
    Last Post: 04-21-2008, 01:33 AM
  5. find first occurance of a string in a file
    By jstn in forum C Programming
    Replies: 2
    Last Post: 05-08-2002, 08:00 PM

Tags for this Thread