How to read integers elegantly using C++ stream?

后端 未结 6 1325
醉话见心
醉话见心 2021-01-02 08:52

I have a file full of lines in this format:

1 - 2: 3

I want to only load numbers using C++ streams. Whats the most elegant way to do it? I

6条回答
  •  盖世英雄少女心
    2021-01-02 09:15

    Simply,

    ifstream file("file.txt");
    int n1, n2, n3;
    char tmp;
    while (file.good()) {
      file >> n1 >> tmp >> n2 >> tmp >> n3;
    }
    

提交回复
热议问题