如何从文件数位的数字? [英] How to count the numbers of digit from a file?

查看:120
本文介绍了如何从文件数位的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是错误。什么是错的我的codeS?

 的#includestdafx.h中
#包括stdlib.h中
#包括文件ctype.hINT _tmain(INT ARGC,_TCHAR *的argv [])
{FILE *输入;
INT NUM;
INT numCount = 0;    输入= FOPEN(123.txt,R);    如果(!输入)
    {
      的printf(无文件\\ A \\ n);
      出口(101);
    }
    而((的fscanf(输入,%d个,试验#))== 1)
    的printf(%D,NUM);    如果(ISDIGIT(输入))
        numCount ++;
    的printf(数数数:%d,numCount);    返回0;
}


解决方案

您的逻辑是完全错误的。你应该使用龟etc()读取单个字符,然后用ISDIGIT测试它们()。当龟etc()返回EOF循环应该终止。

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;
}

解决方案

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.

这篇关于如何从文件数位的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆