C++ program to read a txt file and sort data
<p>I'm a physics PhD student with some experience coding in java, but I'm trying to learn C++.</p>

<p>The problem I'm trying to solve is to read in data from a .txt file and then output all the numbers > 1000 in one file and all those &lt;1000 in another. </p>

<p>What I need help with is writing the part of the code which actually reads in the data and saves it to an array. The data itself is only separated by a space, not all on a new line, which is confusing me a bit as I don't know how to get c++ to recognise each new word as an int. I have canabalised some code I have got from various sources online-</p>

<pre><code> #include &lt;iostream&gt;
 #include &lt;string&gt;
 #include &lt;fstream&gt;
 #include &lt;cstring&gt;
 #include&lt;cmath&gt;

using namespace std;

int hmlines(ifstream &amp;a) {
    int i=0;
    string line;
    while (getline(a,line)) {
        cout &lt;&lt; line &lt;&lt; endl;
        i++;
    }
    return i;
}

int hmwords(ifstream &amp;a) {
    int i=0;
    char c;
    a &gt;&gt; noskipws &gt;&gt; c;
    while ((c=a.get()) &amp;&amp; (c!=EOF)){ 
        if (c==' ') {
            i++;
        }
    }
    return i;
}

int main()
{
    int l=0;
    int w=0;
    string filename;
    ifstream matos;
start:
    cout &lt;&lt; "Input filename- ";
    cin &gt;&gt; filename;
    matos.open(filename.c_str());
    if (matos.fail()) {
        goto start;
    }
    matos.seekg(0, ios::beg);
    w = hmwords(matos);
    cout &lt;&lt; w;
    /*c = hmchars(matos);*/

    int RawData[w];
    int n;

    // Loop through the input file
    while ( !matos.eof() )
    {
        matos&gt;&gt; n;
        for(int i = 0; i &lt;= w; i++)
        {
            RawData[n];
            cout&lt;&lt; RawData[n];
        }
    }

    //2nd Copied code ends here
    int On = 0;

    for(int j =0; j&lt; w; j++) {
        if(RawData[j] &gt; 1000) {
            On = On +1;
        }
    }

    int OnArray [On];
    int OffArray [w-On];

    for(int j =0; j&lt; w; j++) {
        if(RawData[j]&gt; 1000) {
            OnArray[j] = RawData[j];
        }
       else {
            OffArray[j] = RawData[j];
        }
    }

    cout &lt;&lt; "The # of lines are :" &lt;&lt; l
         &lt;&lt; ". The # of words are : " &lt;&lt; w 
         &lt;&lt; "Number of T on elements is" &lt;&lt; On;

    matos.close();
}
</code></pre>

<p>But if it would be easier, i'm open to starting the whole thing again, as I don't understand exactly what all the copied code is doing. So to summarise, what I need is it to-</p>

<p>Ask for a filepath in the console
Open the file, and store each number (separated by a space) as an element in a 1D array</p>

<p>I can manage the actual operations myself I think, if I could just get it to read the file the way I need.</p>

<p>Thanks very much</p>
