Search code examples
c++stlstackifstreamargv

How to push into values into a stack from a text file


I am trying to teach myself C++ with some programming assignments. Trying to learn using stacks but I am unsure how to push values from a txt file into a stack.

Let' say I have the following text file:

16 24 25 3 20 18 7 17 4 15 13 22 2 12 10 5 8 1 11 21 19 6 23 9 14

How would I use ifstream,and argv from the command line to push the values into a stack?

Did research and using this as help, but it may not be relevant:

How to push data of different data types into a vector by reading from a file?


Solution

  • Here is something to get you started.

    stack<int> data;
    {
        ifstream file("file.txt");
        int i;
        while (file >> i)
        {
            data.push_back(i);
        }
    }