Reading integers from a text file with words

后端 未结 6 2024
南笙
南笙 2020-12-12 01:48

I\'m trying to read just the integers from a text file structured like this....

ALS 46000
BZK 39850
CAR 38000
//....

using ifstream.

<
6条回答
  •  伪装坚强ぢ
    2020-12-12 02:17

    You can call ignore to have in skip over a specified number of characters.

    istr.ignore(4);
    

    You can also tell it to stop at a delimiter. You would still need to know the maximum number of characters the leading string could be, but this would also work for shorter leading strings:

    istr.ignore(10, ' ');
    

    You could also write a loop that just reads characters until you see the first digit character:

    char c;
    while (istr.getchar(c) && !isdigit(c))
    {
        // do nothing
    }
    if (istr && isdigit(c))
        istr.putback(c);
    

提交回复
热议问题