Search code examples
c++ifstream

C++ ifstream Reading groups of lines


I am trying to get my ifstream to read multiple lines of text and start over again once it reaches a certain character.

For example, myfile.txt has this in it:

300 200 10 10 / 200 300 20 20

I want to use my method LoadImg for everyone 4 lines, the values in those lines being the parameters. I'm not exactly sure how many blocks of 4 lines there will be, but each will be separated with a /.

I'm kind of new to C++ btw.


Solution

  • You can do something like

    int i[4];
    while (ifs >> i[0] >> i[1] >> i[2] >> i[3]) {
      // do some staff here with i[]
    
      ifs.get();  // get the trailing newline character '\n'
      if (ifs.peek() == '/')  // check the next character, because the last group has no '\'
        ifs.get();
    }