I am trying to read a list of integers from a file and store them to std::set container, after that want to output the integers to the console and then generate the output file. I have tried to do it but could not succeed. If I use .erase() in my program then I can see the output on the console and an empty file without text. If I do not use the .erase() then I have run time error of infinite loop. `
#include <iostream>
#include <set>
#include <fstream>
#include <iterator>
using namespace std;
int main()
{
    set<int> myset;
    fstream textfile;
    textfile.open("input.txt");  // opening the file 
    // reading input from file 
    while (!textfile.eof())
    {
        int iTmp = 0;
        textfile >> iTmp;
        myset.insert(iTmp);
    }
    // output to the console
    set<int>::iterator iter = myset.begin();
    while (!myset.empty())
    {
        cout << *myset.begin() << " ";
        myset.erase(myset.begin());
    }
    // writting output to the file
    ofstream out_data("Ahmad.txt");
    while (!myset.empty())
    {
        out_data << *myset.begin() << " ";
    }
    system("pause");
}`
 
     
     
     
    