Leo wahyd
Leo wahyd

Reputation: 207

How to read only chars from ifstream and ignore numbers in c++?

So i have a file that has numbers and a bunch of characters and i wanna store them in my own data type which i call Grid which is basically a two dimensional vector with some useful features that allow me to go ahead and store data without worrying about anything else. anyways here is an example of how the input file will look like:

---sa-fs-gäörq-qwe- f-s

-- p21-2

4-----

i wanna be able to read all these data char by char and store them in my vector except i wanna be able to ignore the numbers. here is a bit of what i have done

int main()
{
    ifstream file;
    file.open("input.txt");
    Grid<char>g(5,5) //initializing 2d vector 5x5
    while(!file.eof())
    {
        for (int i=0; i<5;i++)
             for(int j=0; i<5;j++)
                 file>>(g[i][j]);
    }
 return 0;

}

Upvotes: 1

Views: 558

Answers (1)

Dai
Dai

Reputation: 155015

char c;
while( file >> c ) {

    if( !isdigit( c ) ) {
//  if( ( c >= 'A' && C <= 'Z' ) || ( c >= 'a' && c <= 'z' ) ) {

        // do stuff with c
    }


}

Or to be slightly more idiomatic, use isalpha. This can depend on your C++ environment's locale setting. Ideally you should use a good Unicode library, unless you can make guarantees about your input file.

Upvotes: 2

Related Questions