简体   繁体   中英

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- fs

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM