istringstream - how to do this?

前端 未结 3 1391
逝去的感伤
逝去的感伤 2021-02-04 20:33

I have a file:

a 0 0
b 1 1
c 3 4
d 5 6

Using istringstream, I need to get a, then b, then c, etc. But I don\'t know how to do it because there

3条回答
  •  后悔当初
    2021-02-04 20:58

    This code snippet extracts the tokens using a single loop.

    #include 
    #include 
    #include 
    
    int main(int argc, char **argv) {
    
        if(argc != 2) {
            return(1);
        }
    
        std::string file = argv[1];
        std::ifstream fin(file.c_str());
    
        char i;
        int j, k;
        std::string line;
        std::istringstream iss;
        while (std::getline(fin, line)) {
            iss.clear();
            iss.str(line);
            iss >> i >> j >> k;
            std::cout << "i=" << i << ",j=" << j << ",k=" << k << std::endl;
        }
        fin.close();
        return(0);
    }
    

提交回复
热议问题