简体   繁体   English

如何仅从ifstream中读取字符并忽略c ++中的数字?

[英]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. 所以我有一个包含数字和一堆字符的文件,我想将它们存储在我自己的数据类型中,我称之为Grid,它基本上是一个二维向量,具有一些有用的功能,可以让我继续进行数据存储而不必担心还要别的吗。 anyways here is an example of how the input file will look like: 无论如何,这是输入文件的示例:

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

-- p21-2 -第21-2页

4----- 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 . 或者更习惯一点,请使用isalpha This can depend on your C++ environment's locale setting. 这可能取决于您的C ++环境的语言环境设置。 Ideally you should use a good Unicode library, unless you can make guarantees about your input file. 理想情况下,除非可以保证输入文件没有问题,否则应使用良好的Unicode库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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