Read integers from file - line by line

前端 未结 4 1075
遥遥无期
遥遥无期 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:44

    You can just use file >> number for this. It just knows what to do with spaces and linebreaks.

    For variable-length array, consider using std::vector.

    This code will populate a vector with all numbers from a file.

    int number;
    vector numbers;
    while (file >> number)
        numbers.push_back(number);
    

提交回复
热议问题