-2

I am having trouble finding a segmentation fault in my code, I am new to coding and do not have well trained eyes. Help is greatly appreciated!

#include <stdio.h>
#include <ctype.h>

int main(void){

    FILE *input_file;

    int num0 = 0, num1 = 0, num2 = 0, num3 = 0, num4 = 0;
    int num5 = 0, num6 = 0, num7 = 0, num8 = 0, num9 = 0;
    char c;


    input_file=fopen("data.txt", "r");

    while (!feof(input_file)) {

        c = fgetc(input_file);

        if (isdigit(c)) {

            if (c=='0') num0++;
                else if (c=='1') num1++;
                else if (c=='2') num2++;
                else if (c=='3') num3++;
                else if (c=='4') num4++;
                else if (c=='5') num5++;
                else if (c=='6') num6++;
                else if (c=='7') num7++;
                else if (c=='8') num8++;
                else if (c=='9') num9++;
        }

        else if (c=='E'){
            c = fgetc(input_file);
            if (c=='N'){
                c = fgetc(input_file);
                if (c=='D') {
                    break;
                }
            }
        }
    }


    printf("Number of 0: %d\n", num0);
    printf("Number of 1: %d\n", num1);
    printf("Number of 2: %d\n", num2);
    printf("Number of 3: %d\n", num3);
    printf("Number of 4: %d\n", num4);
    printf("Number of 5: %d\n", num5);
    printf("Number of 6: %d\n", num6);
    printf("Number of 7: %d\n", num7);
    printf("Number of 8: %d\n", num8);
    printf("Number of 9: %d\n", num9);

They want me to not have so much code without writing
fclose(input_file);

    }
Kara
  • 6,115
  • 16
  • 50
  • 57
  • fclose in the comment above is supposed to be in the code – Alex Germ Mar 02 '15 at 22:19
  • 2
    There's an edit link under the question. – Celeo Mar 02 '15 at 22:22
  • 1
    Where are you getting the segfault? You should be able to use `gdb` to trace the fault to the exact line that is breaking your code. – dub stylee Mar 02 '15 at 22:34
  • 1
    Learn to use arrays. DRY. Also, only read once per loop. – Lee Daniel Crocker Mar 02 '15 at 22:36
  • 1
    How about checking to make sure that file opened? Its a key piece of code in this malaise that actually stands a chance of propagating a NULL pointer because you assumed the file actually opened when in fact it didn't. Assumption is the mother of all.... And closely related, this: `while (!feof(input_file))` [is wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong). – WhozCraig Mar 02 '15 at 23:33
  • 2
    Compile with all warnings and debug info (e.g. `gcc -Wall -Wextra -g`). Improve your code till you get no warnings. Then **use the debugger** (`gdb`). – Basile Starynkevitch Apr 10 '15 at 16:04

2 Answers2

1

Make sure the input file exists. If it doesn't, or it is inaccessible, fopen returns NULL. Attempting to operate on a NULL file wi crash your program.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
0

There are several reads (fgetc) in the loop with single EOF check. Some of the reads may attempt reading an ended file, if the previous read reached the EOF.

Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61