Read integers from file - line by line

前端 未结 4 1065
遥遥无期
遥遥无期 2020-12-11 20:07

How can I read integers from a file to the array of integers in c++? So that, for example, this file\'s content:

23
31
41
23

would become:<

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-11 20:47

    Here's one way to do it:

    #include 
    #include 
    #include 
    
    int main()
    {
        std::ifstream file("c:\\temp\\testinput.txt");
        std::vector list;
    
        std::istream_iterator eos, it(file);
    
        std::copy(it, eos, std::back_inserter(list));
    
        std::for_each(std::begin(list), std::end(list), [](int i)
        {
            std::cout << "val: " << i << "\n";
        });
        system("PAUSE");
        return 0;
    }
    

提交回复
热议问题