How to count the numbers of digit from a file

cio

It's error. What's wrong of my codes?

#include "stdafx.h"
#include "stdlib.h"
#include "ctype.h"

int _tmain(int argc, _TCHAR* argv[])
{

FILE* input;
int num;
int numCount = 0;

    input = fopen("123.txt", "r");

    if (!input)
    {
      printf("No file \a\n");
      exit (101);
    }


    while ((fscanf(input, "%d", &num)) == 1)
        printf("%d", num);

    if (isdigit(input))
        numCount++;


    printf("number count: %d", numCount); 

    return 0;
}

Best Answer

Your logic is completely wrong. You should read individual characters using fgetc() and then test them with isdigit(). The loop should terminate when fgetc() returns EOF.