tags:

views:

473

answers:

4

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;
}
+1  A: 
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++;
}
Erkan Haspulat
Thanks.But it error again."Debug Assertion Failed."
Programme Newbie
num is being set by "%d" in the fscanf and isdigit is looking for ASCII values of the 0-9 characters. The "%d" should be changed to "%c" as well, or better yet fgetc
Oz
A: 

I modified your code and its working now. You did not use command line arguments in your code, so argc and argv were not needed(however that would not cause any problem).

#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
int main(void)
{

   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\n", num);
      if (isdigit(num))
       numCount++;
    }
    /* The function isdigit() returns non-zero if its argument is a digit between 0 and  9. Otherwise, zero is returned.Hence the value of numCount is 0 if number is not between 0 and 9*/



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

  return 0;

}

Prasoon Saurav
Thanks, no error now.But the 'numCount' isn't work, the numCount still 0 although there are "123" in the text file.
Programme Newbie
isdigit() is used to test character codes. you are using it to test an integer value. This does not make much sense.
anon
yes the value of numCount will remain zero because 123 is not between 0 and 9 and hence isdigit() returns zero and the value of numCount is not incremented.
Prasoon Saurav
Oz
The numCount still zero although I change "123" to "1 2 3", "1,2,3", "1, 2, 3" or only "1"
Programme Newbie
@Neil:I dont understand why I got a downvote. I was trying to make him(OP) understand why the output he got was zero.I just made his code error free and explained in my code the reason for the output.
Prasoon Saurav
@Prasoon Check man for isdigit() - then you understand the downvotes.
qrdl
@qrdl: Didn't I write about isdigit() in my post itself?
Prasoon Saurav
@Prasoon - no, you are using isdigit incorrectly. `isdigit(5)` will return false, `isdigit('5')` will return true.
R Samuel Klatchko
+2  A: 

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.

anon
Please tell me why I got a downvote.
Prasoon Saurav
How should I know? It was nothing to do with me.
anon
Ok,thanks .... :)
Prasoon Saurav
+1  A: 

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

Pavel Radzivilovsky