我如何同时读取一系列字符值作为字符和整数,并可以比较它?

时间:2013-09-24 16:20:10

标签: c++ c

我有一个包含一系列字符的文件,如:

......//////////0000000111111111222222222aaaaaaaaaaccccccccccccclllllllllllllllll

我必须逐个扫描它,并且必须比较它是否是一个数字,但是以整数的形式。

我这样用过:

int x=0;
fscanf(fp,"%d",&x)
if (x>=0 && x<=9)

我必须以整数形式读取文件中的数字,并且必须比较它。

2 个答案:

答案 0 :(得分:1)

在c ++中:

#include <iostream>
#include <locale>
char c;
int i;
while(std::cin >> c) {
    if(isdigit(c)) {
        i = c - '0';
    } else {
        //TODO:
    }
}

答案 1 :(得分:1)

C ++答案:

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
    ifstream in_file("chars.txt");
    char c;

    while (in_file >> c)
        if (isdigit(c))
            cout << c << endl;
}