1

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;

}
Leo wahyd
  • 207
  • 1
  • 14
  • 2
    https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Baum mit Augen Sep 09 '16 at 18:44
  • @BaummitAugen im sorry but i couldn't find any relevance between my question and the question in the link, would you help me out here? – Leo wahyd Sep 09 '16 at 18:47
  • 1
    That's not supposed to answer the question, but points out an unrelated bug in your code. That's why I posted it as a comment instead of an answer. ;) – Baum mit Augen Sep 09 '16 at 18:48

1 Answers1

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

Dai
  • 141,631
  • 28
  • 261
  • 374
  • @IvanRubinson Please elaborate. If it's regarding performance, my code assumes the file stream is buffered. – Dai Sep 09 '16 at 18:57
  • i know that @baum mit augen already linked me to answers that are relevant to what i am about to ask, but why exactly not using `while(! file.eof())`? – Leo wahyd Sep 09 '16 at 19:01
  • 2
    The question is about **ignoring the numbers**. Why don't we do this `if( ! (c > '0' && c < '9') ) { /*code*/ }`. That should take care of other Unicode characters without using Unicode library. @Dai – Parnab Sanyal Sep 09 '16 at 19:05
  • @Leowahyd, your comment asking about `file.eof()` sounds like a new question that isn't really related to this one... You'll want to start the question asking process over. (Though, you shouldn't get to the actual posting of a new question. You should find the answer elsewhere as part of the question asking process.) – chwarr Sep 09 '16 at 19:06
  • @Leowahyd No point to asking that question. It will be instantly firebombed and closed as a previously well-answered question. The short version is You check to see if you've reached the end of the file **BEFORE** reading from it. The only way to find whether or not you've hit the end of the file is to read the end of the file. In other words, you need to test for EOF **AFTER** the read, but before you use what you read in case you didn't read anything. In fact, it's better to test for all failures before using what you read because a lot more than just hitting the end of the file can happen. – user4581301 Sep 09 '16 at 19:22
  • This is precisely what Dai does with `while( file >> c )`. Translated to English this means "Read from `file` into `c`. If the read was successful, enter the body of the loop." If the read fails for any reason, the body (the `{}` delimited block beneath the loop) is not entered and the program continues on the next line after the body. At this point you can query `file` to see why the read failed if you want. – user4581301 Sep 09 '16 at 19:27
  • @user4581301 Thank you a lot for such an awesome and informative answer. I know I am way out of the scope of my question, but given the circumstances that you need to fill a multidimensional vector with data from a file, lets a 2d vector for the sake of argument, you will have to use a 2 for loops inside the while loop using. and simply doesn't work right? so there is this solution `file.eof()` and `file.good()` – Leo wahyd Sep 09 '16 at 19:48
  • @Leowahyd Answering that requires a lot more information, particularly how the file is laid out. Recommend a look at [Read File Line By Line](http://stackoverflow.com/questions/7868936/read-file-line-by-line) for a good general purpose solution. – user4581301 Sep 09 '16 at 21:50
  • @Leowahyd I've updated my code. I assumed by "ignore numbers" the OP really meant "use letters" - because why would you want linebreaks to be included, for example? (His example file has linebreaks in it which his current code does not account for). – Dai Sep 10 '16 at 00:52