Reputation: 123
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;
}
Upvotes: 0
Views: 5009
Reputation: 19114
isdigit() tests a character, not a FILE*.
And if you are going to count something, numCount++ should be inside a loop, and not once in the whole program
Upvotes: 1
Reputation:
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.
Upvotes: 2
Reputation: 12562
while ((fscanf(input, "%d", &num)) == 1)
printf("%d", num);
if (isdigit(input))
numCount++;
You are only checking input
once. And you shoul be checking num
instead, input
is a FILE
.
while ((fscanf(input, "%d", &num)) == 1){
printf("%d", num);
if (isdigit(num))
numCount++;
}
Upvotes: 1