0

For my C class I have to write a program that counts the frequency of each digit(0-9) that is in a file. I wrote the code in Java already but cant translate it into C. Any help would be appreciated. I will add what I have so far below. A portion of the text file was added to the end of the code to show what it looks like, a random string of symbols and numbers.

 #include<stdio.h>
    #define m 100
    #define n 60

    void main() {
    char file_name[m]; FILE *input_file;
    char symb, symb0, symb1, symb2, symb3, symb4, symb5, symb6;
    char symb7, symb8, symb9;
    int i;

    printf("Enter name of the input file: "); scanf("%s", file_name);
    input_file = fopen(file_name, "r");

    while (input_file == NULL) {
    printf("Error: There is no file \"%s\"\n", file_name);
    printf("Enter file name (or \".\" to exit): "); scanf("%s", file_name);
    if (strcmp(file_name, ".") == 0) return;
    input_file = fopen(file_name, "r");}

    //read each char of the file till the end
    while ((symb=getc(input_file))!=EOF){
             // if the char is between '0'-'9' print it
            if(symb >= '0' && symb <='9'){
                //printf("%c", symb);
                switch(symb){
                    case '0' :
                    symb0++;
                    break;
                    case '1' :
                    symb1++;
                    break;
                    case '2' :
                    symb2++;
                    break;
                    case '3' :
                    symb3++;
                    break;
                    case '4' :
                    symb4++;
                    break;
                    case '5' :
                    symb5++;
                    break;
                    case '6' :
                    symb6++;
                    break;
                    case '7' :
                    symb7++;
                    break;
                    case '8' :
                    symb8++;
                    break;
                    case '9' :
                    symb9++;
                    break;
                }
                            printf("%c", symb3);
                }
    }

    printf("\n-= Count the Thisles in =-");

    fclose(input_file);
    }
    //...1..1.'`.1.........2..2..2...../\......./%%%%\/%%\.3...4..
    //......'............../\........./%%\../\./%%%%%%/\%%\33..4..
    //...'.../\.........../%%\.../\.../%%\./%%\%/\%/\/%%\%/\......
    //.'..../%%\./\......./%%\../%%\./%%%%\/%%\/%%\%%\%%%/%%\.55..
Shea Shea
  • 11
  • 1
  • 2
    How about an array of ten elements, one for each digit. Initialize each element to zero, increase when a digit is found. And remember that e.g. `'2' - '0' == 2`. – Some programmer dude Nov 04 '17 at 18:39
  • 1
    1) Do you know what the initial `symb1` to `symb9` values are? They're undefined 2) You are incrementing char variables, that's probably not what you had in mind – edgars Nov 04 '17 at 18:41
  • 1
    Not bad at all...nearly there. Declare `symb` as `int` to be able to check for EOF. Also the other symbs must be ints and initialize `symb0= 0` through `symb9= 0`. Should work then. – Paul Ogilvie Nov 04 '17 at 18:42
  • 1
    With `symb3` being an int (or at least used as int), use: `printf("%d", symb3);` – Paul Ogilvie Nov 04 '17 at 18:45
  • As well as using an array of 10 elements to count the digits (and dumping the `switch` method) you need `int symb` and not `char symb`. Look up the man page for `getc` to see what its return type is. – Weather Vane Nov 04 '17 at 18:46
  • 1
    Minor: `void main()` ==> `int main(void)` – Weather Vane Nov 04 '17 at 18:48

1 Answers1

1

Collecting al the comments:

#include<stdio.h>
#define m 100
#define n 60

int main(void) {
    char file_name[m]; FILE *input_file;
    int symb, symbcount[10]={0};
    int i;

    printf("Enter name of the input file: "); scanf("%s", file_name);
    input_file = fopen(file_name, "r");

     while (input_file == NULL) {
        printf("Error: There is no file \"%s\"\n", file_name);
        printf("Enter file name (or \".\" to exit): "); scanf("%s", file_name);
        if (strcmp(file_name, ".") == 0) return 1;
        input_file = fopen(file_name, "r");
    }

    while ((symb=getc(input_file))!=EOF){
        if (symb >= '0' && symb <='9')
            symbcount[symb-'0']++;
    }
    fclose(input_file);

    for (i=0; i<10; i++)
        printf("%d: %d\n", i, symbcount[i]);

    return 0;
}
Paul Ogilvie
  • 24,146
  • 4
  • 18
  • 39
  • Thank you guys! I think I have everything I need to complete my code, so much easier than I was thinking. My only question is: Why use `int main(void)` rather than `void main()` ? – Shea Shea Nov 04 '17 at 19:20
  • `if (strcmp(file_name, ".") == 0) return;` --> `if (strcmp(file_name, ".") == 0) return 0;` – MFisherKDX Nov 04 '17 at 19:21
  • @SheaShea so your program can return an exit code. https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c. For instance, in my comment above, if you wanted to return a code of "0=success" and "1=user was unable to provide a valid name", then you could `return 1` at my comment above. – MFisherKDX Nov 04 '17 at 19:26
  • @SheaShea the C standard says so. ***5.1.2.2.1 Program startup** 1 The function called at program startup is named `main`. The implementation declares no prototype for this function. It shall be defined with a return type of `int` and with no parameters: `int main(void) { /* ... */ }` or with two parameters (referred to here as `argc` and `argv`, though any names may be used, as they are local to the function in which they are declared): `int main(int argc, char *argv[]) { /* ... */ }` or equivalent; or in some other implementation-defined manner.* The last is say embedded app with no return. – Weather Vane Nov 04 '17 at 19:43